从存储过程中提出真正的SQL错误?

时间:2013-10-11 14:21:10

标签: sql sql-server

我有以下存储过程,我想返回实际的SQL Server错误,而不是我的消息,怎么办呢?

我目前正在使用RAISERROR并显示一条消息,但我想返回实际的SQL错误。

由于

ALTER PROCEDURE [dbo].[sp_DBA_DROPFIELD] 
(
    @fieldDisplayName varchar(max),
    @tableSeq int,
    @viewName varchar(max),
    @isVirtual varchar(4)
)
AS
  DECLARE   
    @sql_table_name varchar(max),
    @sql_fieldname varchar(max),
    @sql_columnName varchar(max),
    @sql_fieldSeq int,
    @sql_SEQ_FKTABLE int,
    @sql_FKTABLENAME varchar(max),
    @sql_EXEC_DROPCONSTRAINT varchar(max),
    @sql_EXEC_DROPCOLUMN varchar(max),
    @sql_EXEC_DELETEVIRTUAL varchar(max),
    @sql_EXEC_DELETEFIELDREF int
BEGIN TRAN
BEGIN TRY
select @sql_table_name = TABLENAME from SD_TABLES where SEQUENCE = + @tableSeq
select @sql_columnName = COLUMN_NAME, @sql_fieldSeq = SEQUENCE, @sql_SEQ_FKTABLE = FK_TABLE from SD_FIELDS where DISPLAY_NAME = @fieldDisplayName AND SEQ_TABLE = @tableSeq

if @sql_SEQ_FKTABLE IS NOT NULL
BEGIN
Select @sql_FKTABLENAME = TABLENAME from SD_TABLES where SEQUENCE = @sql_SEQ_FKTABLE
SET @sql_EXEC_DROPCONSTRAINT = 'ALTER TABLE ' + @sql_table_name + ' DROP CONSTRAINT [FK__' + @sql_table_name + '___' + @sql_FKTABLENAME + '_]'
SET @sql_EXEC_DROPCOLUMN = 'ALTER TABLE ' + @sql_table_name + ' DROP COLUMN ' +  @sql_columnName
EXEC sp_executesql @sql_EXEC_DROPCONSTRAINT
EXEC sp_executesql @sql_EXEC_DROPCOLUMN
END

if @isVirtual = 'true'
BEGIN
SET @sql_EXEC_DELETEVIRTUAL = 'ALTER TABLE ' + @sql_table_name + ' DROP COLUMN ' + @sql_columnName
EXEC sp_executesql @sql_EXEC_DELETEVIRTUAL 
END

SET @sql_EXEC_DELETEFIELDREF = 'DELETE FROM SD_FIELDS WHERE SEQUENCE = ' + @sql_fieldSeq
EXEC sp_executesql @sql_EXEC_DELETEFIELDREF

COMMIT 

END TRY

BEGIN CATCH
    ROLLBACK TRAN
   RAISERROR('An error has occured, no changes have been made', 5, 1)
END CATCH          

1 个答案:

答案 0 :(得分:0)

我建议你创建这个存储过程:

CREATE PROCEDURE [dbo].[RethrowError]
AS
-- Return if there is no error information to retrieve.
IF ERROR_NUMBER() IS NULL
    RETURN;

DECLARE 
    @ErrorMessage    NVARCHAR(4000),
    @ErrorNumber     INT,
    @ErrorSeverity   INT,
    @ErrorState      INT,
    @ErrorLine       INT,
    @ErrorProcedure  NVARCHAR(200);

-- Assign variables to error-handling functions that 
-- capture information for RAISERROR.
SELECT 
    @ErrorNumber = ERROR_NUMBER(),
    @ErrorSeverity = ERROR_SEVERITY(),
    @ErrorState = ERROR_STATE(),
    @ErrorLine = ERROR_LINE(),
    @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');

-- Build the message string that will contain original
-- error information.
SELECT @ErrorMessage = 
    N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 
        'Message: '+ ERROR_MESSAGE();

-- Raise an error: msg_str parameter of RAISERROR will contain
-- the original error information.
RAISERROR 
    (
    @ErrorMessage, 
    @ErrorSeverity, 
    1,               
    @ErrorNumber,    -- parameter: original error number.
    @ErrorSeverity,  -- parameter: original error severity.
    @ErrorState,     -- parameter: original error state.
    @ErrorProcedure, -- parameter: original error procedure name.
    @ErrorLine       -- parameter: original error line number.
    );

然后像这样使用它:

BEGIN CATCH
    ROLLBACK TRAN
    EXEC dbo.RethrowError
END CATCH