1

这可能是一个简单的问题,但我需要运行报告的行为来触发存储过程的“预事件”。我没有从该过程返回数据,它通过从 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;
4

1 回答 1

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
于 2013-06-27T14:53:53.460 回答