2009-07-31 76 views
0

我在SSIS中運行「執行SQL任務」 它運行一個存儲過程,執行一些驗證 在存儲過程中,當出現錯誤時,我有一個RAISERROR命令。 但是,當我爲此測試時,此任務無法中止。 我對此有Google搜索,並找到很多參考,但沒有解決方案,適合我。 我已經將我的SQL Server 2005升級到了Service Pack 3,但這沒有任何區別。 一個引用建議在引發異常時加入PRINT語句,這是行不通的。 那麼我該如何解決這個問題? 存儲過程中的代碼是;SSIS(SQL Server 2005)不捕獲SQL異常

ALTER PROCEDURE [dbo].[usp_VAL_Journey] 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    DECLARE @Month AS INT 
        , @Year AS INT 
    SELECT TOP 1 @Year = DATEPART(YEAR, Date), @Month = DATEPART(MONTH, Date) 
      FROM dbo.JourneyLandingTable 

    SELECT TOP 1 * 
      FROM dbo.JourneyMasterTable 
      WHERE DATEPART(YEAR, Date) = @Year 
      AND DATEPART(MONTH, Date) = @Month 
    IF @@ROWCOUNT > 0 
    BEGIN 
      RAISERROR('JourneyMasterTable already contains data for this month.', 16, 1) 
      RETURN 
    END 

    SELECT DATEPART(YEAR, Date) AS year1, DATEPART(MONTH, Date) AS month1 
    FROM dbo.JourneyLandingTable 
    GROUP BY DATEPART(YEAR, Date), DATEPART(MONTH, Date) 

    IF @@ROWCOUNT > 1 
    BEGIN 
      RAISERROR('JourneyLandingTable contains data for more than 1 month.', 16, 1) 
    END 
END 

回答

0

我有類似的事情,但對我來說很好。不知道爲什麼它沒有。我有的設置是我不會在各個地方引起恐慌。我不斷增加錯誤計數並最終提高它如下。它完美的工作 - 它放棄執行和所有的好東西。

declare @errorString varchar(100) 
IF @rc > 0 
BEGIN 
    set @errorString = 'Error(s) occured when trying to run sp_blahblah error count ' + cast(@rc as varchar(10)) 
    raiserror(@errorString , 16, 1) 
END 
0

,你可能會想嘗試一些返回值「RETURN N」

ALTER PROCEDURE [dbo].[usp_VAL_Journey] 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    DECLARE @Month AS INT 
        , @Year AS INT 
    SELECT TOP 1 @Year = DATEPART(YEAR, Date), @Month = DATEPART(MONTH, Date) 
      FROM dbo.JourneyLandingTable 

    SELECT TOP 1 * 
      FROM dbo.JourneyMasterTable 
      WHERE DATEPART(YEAR, Date) = @Year 
      AND DATEPART(MONTH, Date) = @Month 
    IF @@ROWCOUNT > 0 
    BEGIN 
      RAISERROR('JourneyMasterTable already contains data for this month.', 16, 1) 
      RETURN 10 --<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    END 

    SELECT DATEPART(YEAR, Date) AS year1, DATEPART(MONTH, Date) AS month1 
    FROM dbo.JourneyLandingTable 
    GROUP BY DATEPART(YEAR, Date), DATEPART(MONTH, Date) 

    IF @@ROWCOUNT > 1 
    BEGIN 
      RAISERROR('JourneyLandingTable contains data for more than 1 month.', 16, 1) 
      RETURN 20 --<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
    END 
END 

你應該能夠告訴我們,如果該代碼塊是通過檢查過程的返回值擊中。