如何从SQL RaiseError返回实际的错误消息,而不是" Msg 50000"?

时间:2015-10-13 23:21:05

标签: c# sql-server

给出以下存储过程:

CREATE PROCEDURE [dbo].[MergeCustomers]
    @CustomerToKeep UNIQUEIDENTIFIER
  , @CustomerToDelete UNIQUEIDENTIFIER
AS 
    DECLARE @ERROR_MSG NVARCHAR(MAX)
      , @SEVERITY INT
      , @STATE INT
      , @CustomError NVARCHAR(MAX);

    BEGIN TRY
        BEGIN TRANSACTION;

        BEGIN
            SET NOCOUNT ON;

            BEGIN
                /* 1. Check for potential Duplicate Subscriptions if they exist, rollback. */
                IF EXISTS ( SELECT  [S].[SubscriptionType]
                            FROM    [dbo].[subscribers] AS [S]
                            WHERE   ( [S].[UserID] = @CustomerToKeep )
                                    OR ( [S].[UserID] = @CustomerToDelete )
                            GROUP BY [S].[SubscriptionType]
                            HAVING  COUNT([S].[SubscriptionType]) > 1 )
                    BEGIN
                        SET  @CustomError = N'Customers cannot be merged. These customers have potential duplicate'
                                + 'subscriptions, and might need a refund to occur before merging. '
                                + 'Please contact Support.' + ERROR_MESSAGE(); 
                        RAISERROR(@CustomError, 16, 1) --change to > 10
                    END 
            END                
        END

        COMMIT TRANSACTION;

    END TRY

    BEGIN CATCH
        DECLARE @SQLErrorMessage NVARCHAR(2048);
        SET @SQLErrorMessage = ERROR_MESSAGE();
        RAISERROR (@SQLErrorMessage, 16, 1);

        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
    END CATCH
GO

我尝试做的是引发自定义错误,并在引发时返回该错误,但是当我运行proc时我得到的只是:

  

Msg 50000,Level 16,State 1,Procedure MergeCustomers,Line 90

我将从C#Winform中调用此proc,并且我希望确实有真正的错误消息,所以我知道它是什么,并且可以向用户显示。而且我也想在Proc中测试它。

我期待,当我在SQL中运行此proc作为

EXECUTE [dbo].[MergeCustomers] @CustomerToKeep = '6B88274A-38F0-11D5-9D8A-00A0C9D7DEE4', -- uniqueidentifier
    @CustomerToDelete = '06AB5121-A87D-11D5-9D9D-00A0C9D7DEE4' -- uniqueidentifier

看到如下消息:

  

客户无法合并。这些客户可能具有重复订阅,并且可能需要在合并之前进行退款。请联系支持部门。

2 个答案:

答案 0 :(得分:1)

您可以简单地使用try catch块并捕获Exception并使用它的Message属性。它包含您的错误消息。

要测试您可以创建此过程:

Create PROCEDURE [dbo].[TestProcedure]
AS
RAISERROR(N'Some error', 16, 1)
RETURN 0

然后以这种方式测试:

var connection = new SqlConnection(@"Your connection string");
var command = new SqlCommand("dbo.TestProcedure", connection);
command.CommandType = CommandType.StoredProcedure;
try
{
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    if (connection.State == ConnectionState.Open)
        connection.Close();
}

答案 1 :(得分:1)

从第一个SET语句中删除+ ERROR_MESSAGE()。此时没有错误消息,因此引发的错误消息为NULL

CREATE PROCEDURE [dbo].[MergeCustomers]
    @CustomerToKeep uniqueidentifier
  , @CustomerToDelete uniqueidentifier
AS
    DECLARE @ERROR_MSG nvarchar(MAX)
      , @SEVERITY int
      , @STATE int
      , @CustomError nvarchar(MAX);

    BEGIN TRY
        BEGIN TRANSACTION;

        SET NOCOUNT ON;

        IF EXISTS ( SELECT  [S].[SubscriptionType]
                    FROM    [dbo].[subscribers] AS [S]
                    WHERE   ( [S].[UserID] = @CustomerToKeep )
                            OR ( [S].[UserID] = @CustomerToDelete )
                    GROUP BY [S].[SubscriptionType]
                    HAVING  COUNT([S].[SubscriptionType]) > 1 )
            BEGIN
                SET @CustomError = N'Customers cannot be merged. These customers have potential duplicate'
                    + 'subscriptions, and might need a refund to occur before merging. '
                    + 'Please contact Support.'; 
                RAISERROR(@CustomError, 16, 1); --change to > 10
            END;

        COMMIT TRANSACTION;

    END TRY

    BEGIN CATCH
        DECLARE @SQLErrorMessage nvarchar(2048);
        SET @SQLErrorMessage = ERROR_MESSAGE();
        RAISERROR (@SQLErrorMessage, 16, 1);

        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION;
    END CATCH;
GO