2013-06-27 26 views
1

這可能是一個簡單的問題,但我需要運行報告以觸發存儲過程的「事件前」行爲。我不是從過程返回數據,而是通過從已從ISAM數據庫導出的.csv文件執行BULK INSERT來更新數據倉庫中的2個表。報表本身使用單獨的查詢從SQL Server表中提取,但導入的數據最終由多個報表使用,因此需要實際更新表。需要在ssrs報告開始時觸發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_'[email protected]+'.csv"' 
SET @NewHistName = 'RENAME C:\transfer\history\ecallhist2.csv "ecallhist_'[email protected]+'.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; 

回答

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 
+0

非常好!那麼我可以更改我的報告,只使用存儲過程而不是現在使用的查詢?這會迫使我重新創建報告嗎?對不起所有的問題,但我通常是一個網絡技術突然被迫做設計師的責任。在此之後,我仍然必須弄清楚如何修改它以接受從網頁提供的輸入參數,以便通過「存儲」進行過濾並將完成的報告作爲彈出的動態.pdf(未存儲在磁盤上)返回他們的瀏覽器可以在本地打印。到目前爲止我已經連續6天拉我的頭髮...... –

+0

是的,你必須改變報告來調用sp而不是select,但只要新sp返回與列相同的列老選擇,你不會做更多的變化...其他itens應該很容易,implment太,但如果你被卡住,然後只是創建另一個問題... :) –

+0

謝謝!非常簡潔易懂的答案。我非常感謝幫助! –