2010-05-19 2 views
0

SQL Server 2005의 BEGIN CATCH ... END CATCH 블록에서 오류가 "처리됨"을 나타내려고합니다. 오류가 있는지 확인하십시오. try ... catch에서 오류가 "처리됨"이라고 SQL Server에 알립니다.

는 것이 가능할까요? 이것을 고려 다음에이 결과

begin transaction 
    begin try 
    begin transaction 

     select cast('X' as bit) 
    commit transaction 
    end try 
begin catch rollback transaction 

    select error_number(), error_message() 
end catch 

commit transaction 

을 :

(0 row(s) affected) 

(No column name) (No column name) 
245 Conversion failed when converting the varchar value 'X' to data type bit. 

(1 row(s) affected) 
Msg 3902, Level 16, State 1, Line 13 
The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. 

감사합니다. A.

+0

중복 트랜잭션이 선언 - 가장 바깥 쪽 트랜잭션 선언을 제거합니다. –

+0

아 - 다시 귀하의 빠른 응답을 주셔서 감사합니다! :) 하지만 나는 그것을 원한다 ... 외부 트랜잭션, 내 말은 ... try-catch는 실제로 루프에 있고 결국에는 실패하지 않은 것이 무엇이든 원한다. –

+0

사실, if T-SQL (저장된 proc)에서 트랜잭션을 시작하거나 롤백하지 않지만 내 응용 프로그램 (C#)에서 시작하면 호출은 궁극적으로 [[현재 트랜잭션을 커밋 할 수 없으며 쓰는 작업을 지원할 수 없습니다. 로그 파일에 저장하십시오. 트랜잭션을 롤백하십시오.] –

답변

2

일부 오류는 마스킹 가능하지 않다. 항상 XACT_STATE()을 검사하고 계속할 수 있는지 확인해야합니다. 특정 오류 (1205 교착 상태가 일반적인 예임)는 트랜잭션을 롤백하고 사용자가 계속할 수 없도록합니다. 당신이 (일을 보존 할 수있는 루프를) 설명 무엇

는 ussualy 세이브 포인트의 도움으로 이루어집니다 : 당신이있어

begin transaction 
begin try 
while @loopcondition 
begin 
    save transaction loop; 
    begin try 
     -- process loop element here 
    end try 
    begin catch 
    if xact_state() = -1 
    begin 
     -- whole transaction is doomed 
     rollback; 
     raiserror ('Aborting', ....); 
    end 
    else if xact_state() = 0 
    begin 
     -- trasaction was aborted by inner loop 
     raiserror ('Aborted inside', ....); 
    end 
    else if xact_state() = 1 
    begin 
     -- this error is recoverable, rollback to the savepoint and continue the loop 
     rollback loop 
    end 
    end catch 
    -- continue loop here 
    fetch next from .... 
/* 
    -- batch commit here if batch committing 
    if @batchsize 
    begin 
     commit; 
     begin transaction 
    end 
*/ 
end 
commit; 
end try 
begin catch 
    -- if we get here, we could not handle the error inside the loop and continue 
    if xact_state() != 0 
    rollback 
    raiserror('failed to process', ...) 
end catch 
관련 문제