SSRS使用SQL存储过程

时间:2013-03-27 19:31:54

标签: sql-server ssrs-2008

我有一个从链接服务器检索数据的存储过程。我可以通过设置单个变量来运行存储过程。结果最终出现在SQL 2008 R2数据库的表中。 SSRS中的数据集使用EXEC dbo.ImagePlaceholder @Name命令调用此存储过程。这是存储过程:

     USE [LagoReporting]


GO
 /****** Object:  StoredProcedure [dbo].[ImagePlaceholder]    Script Date: 03/27/2013 14:22:21    ******/
    SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:Tim Vavra
-- Create date: 3/27/13
-- Description: Stored procedure to create the table necessary to produce a report that shows all images that are listed
-- as placeholders in Lago.  Included in table will be any job which has a street date greater than today - 30.
-- =============================================


ALTER PROCEDURE [dbo].[ImagePlaceholder]
(
@Name varchar(80)
)
AS
BEGIN

    SET NOCOUNT ON;
    SET FMTONLY OFF; 


declare @SQL nvarchar(4000)
declare @KATBEZC varchar(80)

drop table dbo.ImagePH

set @KATBEZC = @Name    

Create Table #ActiveTemp(
    media nvarchar(max),
    div nvarchar(max),
    folio nvarchar(max),
    pgblock nvarchar(max),
    item nvarchar(max),
    itemname nvarchar(max),
    photoinstruction nvarchar(max),
    photodirection nvarchar(max),
    PH nvarchar(100),
    imagename nvarchar(max))

Create Table [dbo].[ImagePH](
    media nvarchar(max),
    div nvarchar(max),
    folio nvarchar(max),
    pgblock nvarchar(max),
    item nvarchar(max),
    itemname nvarchar(max),
    photoinstruction nvarchar(max),
    photodirection nvarchar(max),
    PH nvarchar(100),
    imagename nvarchar(max))

      set @SQL=
      N'insert into #ActiveTemp 
      select media, div,folio,pgblock,item, itemname, photoinstruction, photodirection,PH,imagename
      from  OPENQUERY(LAGO,
      ''SELECT DISTINCT KATBEZC media,
art.artvf1c div,
CONCAT(pagprec,pagpags) folio,
pro.probezc pgblock,
art.artvf2c item,
art.artbezc itemname,
art.artvf7c photoinstruction,
art.artvf6c photodirection,
bld.bldtmps PH,
bld.bldfnmc ImageName
FROM dok,
pro,
art,
aez,
kat,
PAG,
kav,
kom,
bld,
boz
WHERE kavkatkeyi = katkeyi
AND artkavkeyi = kavkeyi
AND aezartkeyi = artkeyi
AND aezkavkeyi = artkavkeyi
AND prokeyi = aezprokeyi
AND prokavkeyi = aezkavkeyi
AND komprokeyi = prokeyi
AND (komkavkeyi = 0 OR komkavkeyi = prokavkeyi)
AND dokkeyi = komdokkeyi
AND (prosnei = doksnli OR prosnei = doksnei)
AND dokkeyi = pagdokkeyi
AND PAGPAVKEYI = KATPAVKEYI
and bldtmps = 1
and bozbldkeyi = bldkeyi
and bozobjkeyi = artkeyi
and katbezc = ''''' + @Name + '''''
and boztyps = 2
Group by kat.katbezc,
CONCAT(pagprec,pagpags),
art.artvf1c,
pro.probezc,
art.artvf2c,
art.artbezc,
kom.kombezc,
art.artvf7c,
art.artvf6c,
bld.bldtmps,
bld.bldfnmc
'')'

    exec (@SQL)

    insert into dbo.ImagePH 
    select * from #ActiveTemp

END

我没有将数据恢复到报告中,但是当我查询数据库时dbo.ImagePH具有所有正确的信息。

提前感谢您的帮助。对于代码格式感到抱歉。

1 个答案:

答案 0 :(得分:1)

sp的结尾是插入查询

insert into dbo.ImagePH 
select * from #ActiveTemp

如果您希望它返回结果,则需要选择查询

另外

  • 您应该使用sp_executesql,以便将查询作为参数化查询执行。
  • 你不应该在任何时候使用动态SQL
  • 填充临时表是没有意义的。只需直接插入dbo.ImagePH。