Q
Problem solve Get help with specific problems with your technologies, process and projects.

Problems with structured exception handling

I recently read your article "Tuning stored procedures: Structured exception handling in SQL Server 2005" and I was trying to implement the concept discussed. It seems the control doesn't enter into the catch block. What should I do?

I recently read your article " Tuning stored procedures: Structured exception handling in SQL Server 2005" and I was trying to implement the concept discussed. However, it seems the control doesn't enter into the catch block. What should I do?
Try a much simpler example first. This example will fail because we attempt to insert a NULL value into a column that does not allow NULLs:

CREATE TABLE&nbspdbo.emp (
empno int NOT NULL)
go
create procedure &nbspdbo.p_emp_insert
(@empno int)
as
begin
 -- CODE THAT MAY FAIL
  begin try

  insert into emp (empno)
            values (@empno)
      end try

 -- EXECUTED IF THERE IS A FAILURE
 begin catch

    SELECT ERROR_NUMBER()as ERROR_NUMBER,
    ERROR_SEVERITY()as ERROR_SEVERITY,
    ERROR_STATE()as ERROR_STATE,
    ERROR_MESSAGE()as ERROR_MESSAGE

 end catch

end

-- First exec should work with a valid empno
exec dbo.p_emp_insert 1003

-- This should fail because the column does not allow NULLs
-- We should see the error information returned
exec p_emp_insert NULL

This was last published in October 2006

Dig Deeper on SQL Server Stored Procedures

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close