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 in Procedure

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-02-20 : 08:16:46
Ritesh writes "Hi,
I am new in creating Procedure normally I worked on the Fat client Now I have to work on the Fat server concept.
Since I want to use @@error variable of SQL SERVER for error traping.
I want to first confirm that when this technique is failed.
So that I take care of those issue during creating the procedure

Thanks in advance.

Ritesh Sinha"

JoeNak
Constraint Violating Yak Guru

292 Posts

Posted - 2006-02-20 : 10:11:26
I think you might have to explain further, I don't quite understand the question.

Check out @@ERROR in BOL.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-20 : 10:22:21
Me too!
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2006-02-20 : 10:46:35
I *think* you are asking about using RETURN functionality from your Stored Procedures, handing back the @@Error value.

Something like


Create Proc up_DoesSomething
as
select 'something' from atable
return @@Error
go

declare @RC int
exec @rc=up_DoesSomething
if @Rc <> 0
print 'SP failed with error: ['+str(@rc)+']'
else
print 'SP succeeded - return code was: ['+str(@rc)+']'



*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-20 : 11:39:30
Nope, don't want to do that, you should use an output variable

CREATE PROC mySproc @rc int AS .....



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

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2006-02-21 : 04:14:37
Brett: Why do you prefer using an explicitly declared return code to using SQL's native return from the SP? While BOL is by no meaans the be-all-and-end-all source of how to do things, it is usuaslly a good guide to usage of function etc., and that is how they choose to use return :

quote:

USE Northwind
GO
-- Create a procedure that takes one input parameter
-- and returns one output parameter and a return code.
CREATE PROCEDURE SampleProcedure @EmployeeIDParm INT,
@MaxQuantity INT OUTPUT
AS
-- Declare and initialize a variable to hold @@ERROR.
DECLARE @ErrorSave INT
SET @ErrorSave = 0

-- Do a SELECT using the input parameter.
SELECT FirstName, LastName, Title
FROM Employees
WHERE EmployeeID = @EmployeeIDParm

-- Save any nonzero @@ERROR value.
IF (@@ERROR <> 0)
SET @ErrorSave = @@ERROR

-- Set a value in the output parameter.
SELECT @MaxQuantity = MAX(Quantity)
FROM [Order Details]

IF (@@ERROR <> 0)
SET @ErrorSave = @@ERROR

-- Returns 0 if neither SELECT statement had
-- an error, otherwise returns the last error.
RETURN @ErrorSave
GO

--A Transact-SQL batch or stored procedure that executes a stored procedure can retrieve the
--return code into an integer variable:

DECLARE @ReturnStatus INT
DECLARE @MaxQtyVariable INT
EXECUTE @ReturnStatus = SampleProcedure @EmployeeIDParm = 9,
@MaxQtyVariable = @MaxQuantity OUTPUT

-- Show the values returned.
PRINT ' '
PRINT 'Return code = ' + CAST(@ReturnStatus AS CHAR(10))
PRINT 'Maximum Quantity = ' + CAST(@MaxQtyVariable AS CHAR(10))
GO



I'm interested to see if there's something I can improve by using explicit outputs for returns...

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-02-21 : 05:15:01
I think the worry is that something other than the SProc returns an "error" code - e.g. if there is a runtime error and SQL returns the RAISERROR code instead of the Return value from the SProc. I can't remember what we decided the upshot of this was last time we discussed it though! And goodness knows what the value of the OUTPUT parameter is either!

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-02-21 : 09:42:29
Ever since I saw SQL Server override what you have set in certain cases...so if it's not reliable, and you code for it, you could get some interesting results....



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

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

Wanderer
Master Smack Fu Yak Hacker

1168 Posts

Posted - 2006-02-21 : 09:48:06
Thanks Brett. Guess I need to re-consider my best-practices and templates again...

*##* *##* *##* *##*

Chaos, Disorder and Panic ... my work is done here!
Go to Top of Page
   

- Advertisement -