@@ERROR in SQL Server 2005

喜夏-厌秋 提交于 2019-12-23 23:09:58

问题


I have learned to use the SCOPE_IDENTITY() instead of just @@IDENTITYto get the last identity value inserted in a given scope, which can be quite useful in high-concurrency scenarios. Is there any equivalent to that function for the @@ERROR variable? I mean, is there any way to make sure that whenever I write

IF (@@ERROR <> 0) RETURN

I am in fact forcing the function to return because of the very last command in this scope caused an error?


回答1:


From Books Online:

@@ERROR only returns error information immediately after the Transact-SQL statement that generates the error.

@@Error is only within the current scope. So it should have the value for whatever sent the proc to the catch block no matter which of several statements was the one that errored.




回答2:


Writing IF (@@ERROR <> 0) after each and every statement is just not going to work. It requires too much discipline. You should move to BEGIN TRY/BEGIN CATCH. Exception handling and nested transactions shows a pattern of T-SQL procedures that handles both exceptions and nested transactions (something to consider in order to make your T-SQL code robust):

create procedure [usp_my_procedure_name]
as
begin
    set nocount on;
    declare @trancount int;
    set @trancount = @@trancount;
    begin try
        if @trancount = 0
            begin transaction
        else
            save transaction usp_my_procedure_name;

        -- Do the actual work here

lbexit:
        if @trancount = 0   
            commit;
    end try
    begin catch
        declare @error int, @message varchar(4000), @xstate int;
        select @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
        if @xstate = -1
            rollback;
        if @xstate = 1 and @trancount = 0
            rollback
        if @xstate = 1 and @trancount > 0
            rollback transaction usp_my_procedure_name;

        raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ;
        return;
    end catch   
end

Also see Error Handling in SQL 2005 and Later for a deeper discussion on this entire topic.



来源:https://stackoverflow.com/questions/6254286/error-in-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!