2009-07-31 111 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 

你应该能够告诉我们,如果该代码块是通过检查过程的返回值击中。