Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 error handling

Author  Topic 

sent_sara
Constraint Violating Yak Guru

377 Posts

Posted - 2011-07-11 : 12:20:33
when i try to execute the below stored procedure
exec usp_Example_ErrorHandler
the below error is thrown:


Msg 207, Level 16, State 1, Procedure usp_Example_ErrorHandler, Line 46
Invalid column name 'notes'.
Msg 266, Level 16, State 2, Procedure usp_Example_ErrorHandler, Line 46
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 2.


notes column is not present in titles table..this is fine.but "Author table"
is not getting rollbacked

can anyone correct the below stored proc:


CREATE PROCEDURE usp_Example_ErrorHandler

/* Example stored procedure to illustrate error handling
* Example:
DECLARE @RC int
EXEC @RC = usp_Example_ErrorHandler
PRINT 'Return value = ' + CONVERT(varchar(10), @RC)
****************************************************************/
AS

DECLARE @myERROR int -- Local @@ERROR
, @myRowCount int -- Local @@ROWCOUNT

SET NOCOUNT ON

BEGIN TRAN
INSERT INTO Authors (au_id, au_fname, au_lname, contract)
VALUES ('222-22-2222'
, 'Andrew'
, 'Novick'
, 1
)

SELECT @myERROR = @@ERROR
IF @myERROR != 0 GOTO HANDLE_ERROR

INSERT INTO titles(title_id, title, type, price,notes, pubdate)
VALUES('WW0790'
, 'Transact-SQL User-Defined Functions'
, 'popular_comp', 49.95
, 'Great book.', '2003-11-04')
SELECT @myERROR = @@ERROR, @myRowCount = @@ROWCOUNT
IF @myERROR != 0 GOTO HANDLE_ERROR

INSERT INTO titleauthor (au_id, title_id)
VALUES('222-22-2222', 'WW0790')
SELECT @myERROR = @@ERROR, @myRowCount = @@ROWCOUNT
IF @myERROR != 0 GOTO HANDLE_ERROR

COMMIT TRAN -- No Errors, so go ahead

RETURN 0

HANDLE_ERROR:
ROLLBACK TRAN
RETURN @myERROR
GO










tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 12:23:30
It's a fatal error, which can't be handled via transactions. Fix your code so that notes column is not in there for the titles table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 12:23:49
You may be able to use TRY/CATCH though, but not sure.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-07-11 : 12:50:00
Post the DDL, but what Tara is saying is that the column [notes] is not in the table [titles] (I don't expect).

Also, DON'T use Return to return your @RC.

Use an output variable. SQL Server may override that value.

And No, it Doesn't seem that even in R2 that they can capture and control this error



CREATE TABLE #myTable99(Col1 int, Col2 char(1))
GO

DECLARE @Error int, @Rowcount int

BEGIN TRY
INSERT INTO #myTable99(Col1, Col2, notes)
SELECT 1, 'a', 'These are my Notes'

SELECT @Error = @@ERROR, @Rowcount = @@ROWCOUNT
END TRY

BEGIN CATCH
SELECT [Error_Line] = ERROR_LINE()
, [Error_Number] = ERROR_NUMBER()
, [Error_Severity] = ERROR_SEVERITY()
, [Error_State] = ERROR_STATE()
, [Error_Message] = ERROR_MESSAGE()
SELECT @Error AS [Error], @Rowcount AS [Rowcount]
END CATCH
GO

DROP TABLE #myTable99
GO




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-07-11 : 12:51:59
wHICH BEGS THE QUESTION, wHAT DOES tRY/cATCH BLOCK DO FOR US OVER STANDARD ERROR HANDLING?

ooops, sorry for the cap locks



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-07-11 : 13:09:54
The benefit of TRY/CATCH is that you can actually catch the error and do something about it. For instance, in my backup stored procedure, without try/catch, it halts the execution of the code. But with try/catch, I can continue processing. Another example is to catch deadlocks and do a retry.

I use try/catch for all of my error handling now. I enclose all my transactions in it too.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-07-11 : 17:55:31
quote:
Originally posted by X002548

Also, DON'T use Return to return your @RC.


Sure there are other ways, but using the RETURN value is a perfectly good way to pass the error code back to the calling environment. In addition to setting the return value I also re-raise the error. Obviously, that all depends on your environment and the contract you have with the calling environment.
Go to Top of Page
   

- Advertisement -