MSSQL2000和MSSQL2005以上版本的异常处理语法是不相同的。
SQL Server 2005以上版本支持结构化异常处理,而MSSQL2000是不支持的。
1)先看MSSQL 2000的异常处理语法:
create proc sp_mssql2000
(@TitleName nvarchar(128))
as
declare @err int,
begin transaction
insert into...
select @err = @@error
if @err <> 0
GOTO ERROR_HANDLER
update...set...
if @err <> 0
GOTO ERROR_HANDLER
GOTO EXIT_Proc
ERROR_HANDLER:
ROLLBACK TRANSACTION
-- Log the error
insert Log (tableName, UserName, errorNumber, errorSeverity, errorState)
values (@tableName, suser_sname(), @err, 0, 0)
EXIT_Proc:
commit tran
2)MSSQL2005的异步处理语法:
create proc sp_mssql2005
(@TitleName nvarchar(128))
as
declare @err int
BEGIN TRY
begin transaction
insert dbo.Authors (au_fname, au_lname, TitleID,CommissionRating)
values (@au_fname, @au_fname, @@IDENTITY, @CommissionRating)
commit transaction
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
-- Log the error
insert into log (UserName, tableName,
errorNumber, errorSeverity, errorState, errorMessage)
values (suser_sname(), @tableName, ERROR_NUMBER(),
ERROR_SEVERITY(), ERROR_STATE(), ERROR_MESSAGE())
RAISERROR (@errormessage, 16,1)
(@TitleName nvarchar(128))
as
declare @err int
BEGIN TRY
begin transaction
insert dbo.Authors (au_fname, au_lname, TitleID,CommissionRating)
values (@au_fname, @au_fname, @@IDENTITY, @CommissionRating)
commit transaction
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
-- Log the error
insert into log (UserName, tableName,
errorNumber, errorSeverity, errorState, errorMessage)
values (suser_sname(), @tableName, ERROR_NUMBER(),
ERROR_SEVERITY(), ERROR_STATE(), ERROR_MESSAGE())
RAISERROR (@errormessage, 16,1)
--返回用户定义的错误信息并设系统标志,记录发生错误。
END CATCH