How do you handle exceptions in SQL Server stored procedure?

How do you handle exceptions in SQL Server stored procedure?

To handle exception in Sql Server we have TRY.. CATCH blocks. We put T-SQL statements in TRY block and to handle exception we write code in CATCH block. If there is an error in code within TRY block then the control will automatically jump to the corresponding CATCH blocks.

How do you handle exceptions in procedures?

To handle other Oracle errors, you can use the OTHERS handler. The functions SQLCODE and SQLERRM are especially useful in the OTHERS handler because they return the Oracle error code and message text….Predefined PL/SQL Exceptions.

Exception Oracle Error SQLCODE Value
ZERO_DIVIDE ORA-01476 -1476

Can we use try catch in stored procedure?

If the stored procedure contains a TRY… CATCH construct, the error transfers control to the CATCH block in the stored procedure. When the CATCH block code finishes, control is passed back to the statement immediately after the EXECUTE statement that called the stored procedure.

What is exception in stored procedure?

An exception is a named error message that can be raised from a stored procedure. Exceptions are created with CREATE EXCEPTION , modified with ALTER EXCEPTION , and dropped with DROP EXCEPTION . A stored procedure raises an exception with EXCEPTION .

How do you optimize stored procedure in SQL Server 2005?

Improve stored procedure performance in SQL Server

  1. Use SET NOCOUNT ON.
  2. Use fully qualified procedure name.
  3. sp_executesql instead of Execute for dynamic queries.
  4. Using IF EXISTS AND SELECT.
  5. Avoid naming user stored procedure as sp_procedurename.
  6. Use set based queries wherever possible.
  7. Keep transaction short and crisp.

What is the use of @@ error in SQL?

Returns an error number if the previous statement encountered an error. If the error was one of the errors in the sys. messages catalog view, then @@ERROR contains the value from the sys. messages.

What is the use of @@ error in SQL Server?

Using @@ERROR to conditionally exit a procedure. The following example uses IF…ELSE statements to test @@ERROR after an DELETE statement in a stored procedure. The value of the @@ERROR variable determines the return code sent to the calling program, indicating success or failure of the procedure.

How do I speed up a stored procedure execution?

  1. Specify column names instead of using * in SELECT statement. Try to avoid *
  2. Avoid temp temporary table. Temporary tables usually increase a query’s complexity.
  3. Create Proper Index. Proper indexing will improve the speed of the operations in the database.
  4. Use Join query instead of sub-query and co-related subquery.

How can check stored procedure performance in SQL Server?

How to test performance of stored procedure in SQL Server

  1. Using SQL Server Profiler.
  2. Display Estimated Execution Plan.
  3. Using SQL Server Profiler.
  4. Using Transact-SQL.
  5. Verify while creating a stored procedure.
  6. Verify the execution of a stored procedure.

How do you optimize stored procedure in SQL server 2005?

How do you troubleshoot a stored procedure?

Debugging options

  1. Start Debugging. To start debugging a SQL server stored procedure in SQL Server, press ALT + F5, or go to Debug -> Start Debugging, as shown in the figure below:
  2. Stepping Through Script.
  3. Run To Cursor.
  4. The Local Window.
  5. The Watch Window.
  6. The Call Stack.
  7. The Immediate Window.
  8. Breakpoints.