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