| 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 procedureThanks 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. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-02-20 : 10:22:21
|
| Me too! |
 |
|
|
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 likeCreate Proc up_DoesSomethingasselect 'something' from atablereturn @@Errorgodeclare @RC intexec @rc=up_DoesSomethingif @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! |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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 NorthwindGO-- Create a procedure that takes one input parameter-- and returns one output parameter and a return code.CREATE PROCEDURE SampleProcedure @EmployeeIDParm INT, @MaxQuantity INT OUTPUTAS-- Declare and initialize a variable to hold @@ERROR.DECLARE @ErrorSave INTSET @ErrorSave = 0-- Do a SELECT using the input parameter.SELECT FirstName, LastName, TitleFROM EmployeesWHERE 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 @ErrorSaveGO--A Transact-SQL batch or stored procedure that executes a stored procedure can retrieve the --return code into an integer variable:DECLARE @ReturnStatus INTDECLARE @MaxQtyVariable INTEXECUTE @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! |
 |
|
|
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 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
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! |
 |
|
|
|