Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Error handling questions

 
   Database Help (Home) -> Programming RSS
Next:  Differences between primary key and unique key  
Author Message
mscertified

External


Since: Jan 10, 2008
Posts: 115



(Msg. 1) Posted: Thu Aug 07, 2008 1:52 pm
Post subject: Error handling questions
Archived from groups: microsoft>public>sqlserver>programming (more info?)

In my sprocs all SQL calls have TRY CATCH
In CATCH, I want to log all error functions ERROR_MESSAGE(), ERROR_NUMBER()
etc. etc. to a table.
Can I call another sproc to do this or is the scope of the error functions
only in the sproc where the error occurs?
(I guess I'm also wary that the called sproc may itself get an error which
overlays the original error)
Also, it seems I cant use the error functions directly in an INSERT..
VALUES.. list but first have to declare a variable and assign the function
return value. Is this right?
How is this best done with minimal code in my sprocs?

Thanks, you guys have been very patient and helpful so far

 >> Stay informed about: Error handling questions 
Back to top
Login to vote
Plamen

External


Since: Jul 14, 2008
Posts: 52



(Msg. 2) Posted: Thu Aug 07, 2008 2:26 pm
Post subject: Re: Error handling questions [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

You can use a stored procedure to log the errors. Look at the examples
in SQL Server Books Online:

USE AdventureWorks;
GO

-- Verify that the stored procedure does not exist.
IF OBJECT_ID ('usp_GetErrorInfo', 'P') IS NOT NULL
DROP PROCEDURE usp_GetErrorInfo;
GO

-- Create a procedure to retrieve error information.
CREATE PROCEDURE usp_GetErrorInfo
AS
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_LINE() as ErrorLine,
ERROR_MESSAGE() as ErrorMessage;
GO

BEGIN TRY
-- Generate divide-by-zero error.
SELECT 1/0;
END TRY
BEGIN CATCH
-- Execute the error retrieval routine.
EXECUTE usp_GetErrorInfo;
END CATCH;
GO


Plamen Ratchev
http://www.SQLStudio.com

 >> Stay informed about: Error handling questions 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Error handling - how to handle if select has error consider this scenario i have created one table create table test(testid int , description varchar(50)) i created sp create proc p_test as select * from list i have renamed table name as table 1 ho...

error handling sp_xml_preparedocument - I've been really struggling with this... I have a customer that's dumping raw XML into a varchar(8000) column on a SQL server 2000 database. I have to write a SP which will run every 5 minutes or so to process the XML and insert the data into other....

Error handling behaviour when using sqlcmd - I have several T-SQL scripts invoked by several perl scripts. My perl scripts check the return code from sqlcmd to detect errors so they can react appropriately. The following excerpt illustrates how I am doing this: my $cmdline = ("sqlcmd -S..

Rollback/transaction/error handling - Hi all, I'm primarily a middle-tier coder who's also administrating the database. I'm not writing any rollback/transaction/error handling code in my sp's. Should I? What are the advantages of inserting this code? What are the dangers of disregarding..

Error handling SQL 2000 - Given 3 INSERT statements in a stored procedure where: The first INSERT runs without errors. The second gives an error like "Error converting data type varchar to float.". The third procedure has no errors and could be executed. Is it possibl...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]