Mega Code Archive

 
Categories / MSSQL Tutorial / Transact SQL
 

Try catch Demo

7>  CREATE TABLE SomeData 8> ( 9>     SomeColumn INT 10> ) 11> GO 1> 2> BEGIN TRANSACTION 3> 4> BEGIN TRY 5>     --Throw an exception on insert 6>     INSERT SomeData VALUES (CONVERT(INT, 'abc')) 7> END TRY 8> BEGIN CATCH 9>     --Try to commit... 10>     COMMIT TRANSACTION 11> END CATCH 12> GO Msg 3930, Level 16, State 1, Server J\SQLEXPRESS, Line 10 The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction. 1> 2> 3> drop table SomeData; 4> GO