USE AdventureWorks2008R2; GO -- Variable to store ErrorLogID value of the row -- inserted in the ErrorLog table by uspLogError DECLARE @ErrorLogID INT; BEGIN TRY BEGIN TRANSACTION; -- A FOREIGN KEY constraint exists on this table. This -- statement will generate a constraint violation error. DELETE FROM Production.Product WHERE ProductID = 980; -- If the delete operation succeeds, commit the transaction. COMMIT TRANSACTION; END TRY BEGIN CATCH -- Call procedure to print error information. EXECUTE dbo.uspPrintError; -- Roll back any active or uncommittable transactions before -- inserting information in the ErrorLog.
-- XACT_STATE = 0 means there is no transaction and
-- a commit or rollback operation would generate an error.IF XACT_STATE() <> 0 BEGIN ROLLBACK TRANSACTION; END EXECUTE dbo.uspLogError @ErrorLogID = @ErrorLogID OUTPUT; END CATCH; -- Retrieve logged error information. SELECT * FROM dbo.ErrorLog WHERE ErrorLogID = @ErrorLogID; GO
'MSSQL' 카테고리의 다른 글
[MSSQL] 데드락 (교착상태) Dead Lock (0) | 2016.07.03 |
---|---|
[MSSQL] 컬럼명(필드명)에 특정 문자열 포함하는 테이블 찾기 (0) | 2016.07.03 |
[MSSQL] TRANSACTION(트랜잭션) / COMMIT(커밋) / ROLLBACK(롤백) (0) | 2016.07.02 |
[MSSQL] 특정문자열 포함된 프로시저 찾기 (0) | 2016.07.02 |
[MSSQL] 데이터 타입 (자료형) 유형 총정리 (0) | 2016.07.02 |