0

您好我正在使用 SQL SERVER 2008 R2 数据中心版,我有这个存储过程,我在其中使用 raiserror 引发自定义错误消息。有没有办法也得到 sql server 的默认错误消息。存储过程的代码如下。

CREATE Procedure usp_SomeName
@StableName         nvarchar(50) = NULL,
@StableID           INT = NULL OUTPUT,
@StableDescription ntext = NULL,
@Picture            image = NULL,
@ReturnCode         int = NULL OUTPUT,
@ReturnMessage      nvarchar(100) = NULL OUTPUT
AS
BEGIN
  SET NOCOUNT ON;
    BEGIN TRY

           IF @StableName IS NULL
             RAISERROR('Validation Failed: Stable name cannot be null', 16,1)

           IF EXISTS (SELECT StableName from dem.TableName
                        WHERE StableName = @StableName)
              RAISERROR('Validation Failed: Stable Name already Exists',16,1)

        BEGIN TRANSACTION
          INSERT INTO TableName(StableName, [Description], Picture)
          VALUES (@StableName, @StableDescription, @Picture)

          COMMIT TRANSACTION
        SELECT @ReturnCode = 0 , @ReturnMessage = 'Sucess! New Stable Details has added.'
        SELECT @StableID = SCOPE_IDENTITY()
    END TRY

    BEGIN CATCH 
       IF (@@TRANCOUNT > 0)
         ROLLBACK TRAN

       SELECT @ReturnCode = ERROR_NUMBER(), @ReturnMessage = ERROR_MESSAGE()

          SELECT @ReturnCode AS ReturnCode, @ReturnMessage AS ReturnMessage,
                 ERROR_LINE()   AS ErrorLine,
                 ERROR_SEVERITY() AS ErrorSeverity,
                 ERROR_STATE() AS ErrorState
    END CATCH

   SET NOCOUNT OFF;
END

GO
4

1 回答 1

0

您可以使用 try catch ,当您引发错误时,批处理会去捕获,然后使用 @@error_message 查看错误消息。

于 2013-10-11T23:00:33.900 回答