需要在ssrs报告开始时触发SQL Server存储过程

时间:2013-06-27 14:39:18

标签: sql-server tsql reporting-services

这可能是一个简单的问题,但我需要运行报告以执行"事件前"触发存储过程。我没有从过程返回数据,它通过从ISAM数据库导出的.csv文件执行BULK INSERT来更新数据仓库中的2个表。报告本身使用单独的查询从SQL Server表中提取,但导入的数据最终由多个报告使用,因此需要实际更新表。

存储过程将作为常规例程的一部分每晚运行,但影响此特定报告的数据将由用户更新,并在运行报告之前立即创建新的.csv提取,因此报告需要触发存储过程在查询这些表本身之前更新表。

我尝试过搜索,但我发现的所有引用似乎都集中在使用存储过程作为报告查询,而这并不是我想要完成的。我有一个单独的数据提取查询,我需要在报表查询之前运行存储过程,如果有意义的话。

有人知道如何触发存储过程作为报表查询的开头行吗?

提前感谢任何想法。我不是一个SQL程序员(或任何类型的程序员,真的)所以请相信你的建议...高级概念假设我自己的任何现有知识基础可能会丢失在我身上。

这是我写的存储过程(dbo.KCSI.DataUpdate),如果有帮助...

--To run as a script (query) the following 2 lines should be un-commented (there are 3 of these 'run-as-a-script' comments to find)
--USE KCSI
--Go

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- To run as a script (query) the following 3 lines should all be commented out
CREATE PROCEDURE DataUpdate
AS
BEGIN

SET NOCOUNT ON 

-- Declare all the needed variables.
DECLARE @CustFile varchar(255)
DECLARE @CustFile_Exists int 
DECLARE @HistFile varchar(255)
DECLARE @HistFile_Exists int
DECLARE @dt varchar(30)
DECLARE @NewCustName varchar(250)
DECLARE @NewHistName varchar(250)

-- Sets Boolean value for whether or not each file exists, using T-SQL extended (i.e. DOS Shell) command
SELECT @CustFile='C:\transfer\ecallcust.csv' 
EXEC Master.dbo.xp_fileexist @CustFile, @CustFile_Exists OUT

SELECT @HistFile='C:\transfer\ecallhist.csv' 
EXEC Master.dbo.xp_fileexist @HistFile, @HistFile_Exists OUT

-- Sets a date variable to append to the final file name
SELECT @dt = REPLACE(Convert(varchar(30),getdate(),120),':','_')
-- Sets a variable to hold the final name. Variable use required because of the hybrid nature of the name (dos shell command + SQL variable)
SET @NewCustName = 'RENAME C:\transfer\history\ecallcust2.csv "ecallcust_'+@dt+'.csv"'
SET @NewHistName = 'RENAME C:\transfer\history\ecallhist2.csv "ecallhist_'+@dt+'.csv"'

-- Subroutine runs only if ecallcust.csv is present
IF @CustFile_Exists = 1
BEGIN

--Zaps the table
TRUNCATE TABLE custextract
-- Initially renames the file, using T-SQL extended (i.e. DOS Shell) command
EXEC master.dbo.xp_cmdshell 'RENAME C:\transfer\ecallcust.csv ecallcust2.csv'

-- Update table from CSV file
BULK INSERT custextract
FROM 'c:\transfer\ecallcust2.csv'
WITH (
ROWTERMINATOR='\n'
)

-- Move file to the history directory and rename it to include the date-time stamp using T-SQL extended (i.e. DOS Shell) command
EXEC master.dbo.xp_cmdshell 'MOVE C:\transfer\ecallcust2.csv C:\transfer\history\'
EXEC master.dbo.xp_cmdshell @NewCustName

END

-- Subroutine runs only if ecallhist.csv is present
IF @HistFile_Exists = 1
BEGIN

--Zaps the table
TRUNCATE TABLE histextract
-- Initially renames the file, using T-SQL extended (i.e. DOS Shell) command
EXEC master.dbo.xp_cmdshell 'RENAME C:\transfer\ecallhist.csv ecallhist2.csv'

-- Update table from CSV file
BULK INSERT histextract
FROM 'c:\transfer\ecallhist2.csv'
WITH (
ROWTERMINATOR='\n'
)

-- Move file to the history directory and rename it to include the date-time stamp using T-SQL extended (i.e. DOS Shell) command
EXEC master.dbo.xp_cmdshell 'MOVE C:\transfer\ecallhist2.csv C:\transfer\history\'
EXEC master.dbo.xp_cmdshell @NewHistName

END

-- To run as a script (query) the following line should be commented out
END
GO

和报告查询...

WITH OrderedYTD AS
(
SELECT custextract.*, histextract.*,
       ROW_NUMBER () OVER (PARTITION BY custextract.custcustno ORDER BY histextract.salesytd desc) AS RowNumber
FROM custextract 
INNER JOIN histextract 
    ON custextract.custcustno = histextract.histcustno
WHERE (custextract.ecall = 'Y')
) 

SELECT OrderedYTD.*
FROM OrderedYTD
WHERE RowNumber <= 10;

1 个答案:

答案 0 :(得分:3)

创建一个存储过程,首先更新数据,然后返回要由报告加载的刷新数据...

CREATE PROCEDURE DataSelect
AS
BEGIN

    -- Refresh Data Here
    EXEC DataUpdate

    -- Select Data for Report
    WITH OrderedYTD AS
    (
        SELECT custextract.*, histextract.*,
   ROW_NUMBER () OVER (PARTITION BY custextract.custcustno ORDER BY histextract.salesytd desc) AS RowNumber
        FROM custextract 
        INNER JOIN histextract 
            ON custextract.custcustno = histextract.histcustno
        WHERE (custextract.ecall = 'Y')
    ) 

    SELECT OrderedYTD.*
    FROM OrderedYTD
    WHERE RowNumber <= 10;

END