SQL Server/T-SQL Tutorial/Transact SQL/Error Handler
This example is a simple stored procedure using a generic approach to error handling:
5> CREATE PROCEDURE spRunSQL
6> @Statement VarChar(2000)
7> AS
8> DECLARE @StartTime DateTime
9> , @EndTime DateTime
10> , @ExecutionTime Int
11> SET @StartTime = GetDate()
12> EXECUTE (@Statement)
13> IF @@Error = 0
14> BEGIN
15> SET @EndTime = GetDate()
16> SET @ExecutionTime = DateDiff(MilliSecond, @StartTime, @EndTime)
17> RETURN @ExecutionTime
18> END
19> GO
1>
2> drop proc spRunSQL;
3> GO