안녕하세요.
프로시저 문에 대한 문의가 있습니다.
아래는 트랜젝션 부분인데.
insert 문과 delete 그리고 다시 insert를 처리해야 합니다.
이러한 쿼리문 중 하나라도 오류가 뜨면 전부다 롤백을 해야 하는데;;
도통 해결이 되지 않아 며칠을 고민하다 문의드립니다.
일부로 마지막 insert문에서 에러가 뜨도록 파라메터를 날렸고 당연히 앞의 insert와 delete가 롤백이 될지 알았는데 ㅠㅠ
실행이 되어버립니다.
DB는 약해서 해결법이나 힌트 좀 알려주시면 감사하겠습니다. 프로그램 업데이트를 못하고 있습니다 ㅠㅠ
BEGIN tran test1
declare @result int
set @result = 0
SET NOCOUNT ON;
insert into HANON_LOT_PRESS select head_qr , head_date , head_num , head_flow , head_model , hn_date , hn_time , hn_num , hn_model from HANON_PRESS where head_qr = @qrcode //insert 성공여부
if(@@Error = 0 and @@rowcount > 0)
begin
delete from hanon_press where head_qr = @qrcode //insert 성공시
if(@@Error = 0 and @@rowcount > 0) //delete 성공여부
begin
insert into hanon_lot_test values(@qrcode , '11' , '11' , '11' , '11' , '111' , '11' , '11' , @hn_model)//delete 성공시
if(@@Error = 0 and @@rowcount > 0)
begin
set @result = 1
select @resultp = @result //최종 반영 여부 return
return
end
else
begin
rollback tran test1//마지막 insert 실패시 롤백
end
end
else
begin
rollback tran test1//delete 실패시 롤백
end
end
else
begin
select @resultp = @result //첫 insert 오류시 반영 여부 return
return
end
commit tran test1
https://msdn.microsoft.com/ko-kr/library/ms188792%28v=sql.120%29.aspx?f=255&MSPPError=-2147217396