Error Handling in SQL Server 2005 : Try catch « Transact SQL « SQL Server / T-SQL Tutorial






4>
5> CREATE PROCEDURE spDeleteProduct @Productid int
6> AS
7> BEGIN TRY
8>  BEGIN TRANSACTION
9>  DELETE Product WHERE ProductID = @ID
10>  COMMIT TRANSACTION
11> END TRY
12> BEGIN CATCH
13>  DECLARE @Err AS int
14>  DECLARE @Msg AS varchar(max)
15>  SET @Err = @@Error
16>  SET @Msg = Error_Message()
17>  ROLLBACK TRANSACTION
18>  INSERT ErrorTable
19>  VALUES (@err, @msg)
20> END CATCH
21> GO

1>
2>
3> drop PROCEDURE spProduct_Delete;
4> GO


1>
2>
3> drop table Product;
4> GO








20.23.Try catch
20.23.1.Error Handling in SQL Server 2005
20.23.2.Error Handling with TRY...CATCH
20.23.3.try...catch Demo
20.23.4.Basic TRY/CATCH
20.23.5.Another TRY/CATCH
20.23.6.Nesting TRY...CATCH Calls
20.23.7.Applying TRY...CATCH Error Handling Without Recoding a Stored Procedure