我有以下存储过程,我想返回实际发生的 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