【问题标题】:SSIS (SQL Server 2005) Does not trap SQL exceptionSSIS (SQL Server 2005) 不捕获 SQL 异常
【发布时间】:2009-07-31 18:34:16
【问题描述】:

我在 SSIS 中运行“执行 SQL 任务” 它运行一个存储过程来进行一些验证 在存储过程中,当出现问题时,我有一个 RAISERROR 命令。 但是,当我对此进行测试时,此任务无法中止。 我对此进行了谷歌搜索并找到了很多参考资料,但没有适合我的解决方案。 我已将我的 SQL Server 2005 升级到 Service Pack 3,但这没有任何区别。 一个参考建议在抛出异常时放入 PRINT 语句,这不起作用。 那么我该如何解决呢? 存储过程中的代码是;

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

【问题讨论】:

    标签: sql-server-2005 ssis


    【解决方案1】:

    我有类似的东西,但对我来说效果很好。不知道为什么它没有。我的设置是我不会在各个地方引发错误。我不断增加错误计数,最后按如下方式提高它。它工作得很好——它中止了执行和所有的好东西。

    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
    

    【讨论】:

      【解决方案2】:

      您可能想尝试返回一些值“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
      

      您应该能够通过检查过程的返回值来判断该代码块是否被命中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-18
        • 2013-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多