Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Intermediate
Feb 25, 2000

Make RAISERROR Your Last Command in Stored Procedures or Triggers

If you execute a SQL statement in a stored procedure after your use the RAISERROR command, you will clear @@ERROR. This is true even if you use a RETURN and return a non-zero value in the stored procedures. For example:
 DECLARE @intReturn int
     , @vcErrorMessage varchar(255)
     , @BeginTransCount int

SELECT @BeginTransCount = @@TRANCOUNT 
-- line above is needed if transaction processing

-- run some SQL command
IF @@error <> 0
BEGIN
     SELECT @intReturn = -1
          , @vcErrorMessage = "An error 
occurred performing some operation."
     GOTO error
END

-- more code...

error:

IF @intReturn IS NULL
BEGIN
     IF @@TRANCOUNT > @BeginTransCount COMMIT  TRANSACTION
END
ELSE
BEGIN
     IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
     RAISERROR ( @vcErrorMessage, 16, 1)
END
GO
David Satz
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date