You are the database administrator for AIOTestKing.com. You create a new stored procedure.
You run the stored procedure and discover an error. You want to gather and process any error information that is generated while running the procedure. Error logging must use the minimal amount of resources while providing the needed functionality. You need to know what caused the error. You also need to include the appropriate error functions in the new stored procedure. What should you do?
A.
Use a GOTO statement.
B.
Set the XACT_ABORT command to off.
C.
Use the @@error function.
D.
Use TRY…CATCH blocks.
Explanation:
Errors in Transact-SQL code can be processed using a TRY…CATCH construct similar to the exception-handling features of the Microsoft Visual C++ and Microsoft Visual C# languages. A TRY…CATCH construct consists of two parts: a TRY block and a CATCH block. When an error condition is detected in a Transact-SQL statement contained in a TRY block, control is passed to a CATCH block where it can be processed. After the CATCH block handles the exception, control is then transferred to the first Transact-SQL statement that follows the END CATCH statement. If the END CATCH statement is the last statement in a stored procedure or trigger, control is returned to the code that invoked the stored procedure or trigger. Transact-SQL statements in the TRY block following the statement that generates an error will not get executed.If there are no errors inside the TRY block, control passes to the statement immediately after the associated END CATCH statement. If the END CATCH statement is the last statement in a stored procedure or trigger, control is passed to the statement that invoked the stored procedure or trigger.