执行sql作业后运行查询

时间:2014-07-03 01:18:35

标签: sql sql-server reporting-services

我正在为SSRS中的报告编写存储过程。

我的问题是我需要在选择其记录之前调用SQL作业来更新表。

目前我正在使用此脚本:

DECLARE @FromDate       DATETIME
       ,@ToDate         DATETIME
       ,@MID            VARCHAR(MAX)

SELECT @FromDate        = '2014-06-13 00:00:00';
SELECT @ToDate          = '2014-06-13 00:00:00';
SELECT @MID             = 'PHP';    

DECLARE @SQLConnect NVARCHAR(500)

SET @SQLConnect = 'SELECT * FROM OPENQUERY([149.122.30.68,52900], ''[REZ5JWB01].[dbo].[ACCOUNTING_SummaryOfRegularRefunds] '''''+CONVERT(NVARCHAR,@FromDate)+''''','''''+CONVERT(NVARCHAR,@ToDate)+''''','+CONVERT(NVARCHAR,@MID)+''')'

BEGIN TRY DROP TABLE #SQLConnect END TRY BEGIN CATCH END CATCH;

CREATE TABLE #SQLConnect
(
    [TransactionDate]   [datetime]      NOT NULL,
    [RecordLocator]     [varchar](6)    NOT NULL,
    [PaymentID]         [bigint]        NOT NULL,
    [AccountNumberID]   [bigint]        NOT NULL,
    [AccountNumber]     [nvarchar](24)  NOT NULL,
    [PaymentAmount]     [money]         NOT NULL,
    [NetAmount]         [money]         NOT NULL
)

INSERT INTO #SQLConnect
EXEC (@SQLConnect)

INSERT INTO CyberSourceFileUpload.dbo.RefundTransactionDetail
SELECT [TransactionDate]
    ,[RecordLocator]
    ,[PaymentID]
    ,[AccountNumberID]
    ,[AccountNumber]
    ,NULL
    ,[PaymentAmount]
    ,[NetAmount]
FROM #SQLConnect

BEGIN TRY DROP TABLE #SQLConnect END TRY BEGIN CATCH END CATCH;

EXEC msdb.dbo.sp_start_job  @job_name = 'Regular Refunds for Manual Processing'

DECLARE @job_status INT
SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''Regular Refunds for Manual Processing''')

WHILE @job_status <> 1
BEGIN
    WAITFOR DELAY '00:00:10'
    SELECT @job_status = current_execution_status FROM OPENROWSET('SQLNCLI', 'Server=.;Trusted_Connection=yes;','exec msdb.dbo.sp_help_job @job_name = ''Regular Refunds for Manual Processing''')
END 

SELECT * FROM CyberSourceFileUpload.dbo.RefundTransactionDetail

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您可以使用sp_start_job从代码运行作业。

以下链接可帮助您使用sp_start_job

sp_start_job (Transact-SQL)

Different ways to execute a SQL Agent job