| Author |
Topic |
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2011-11-18 : 01:34:13
|
| Dear All,I am using following store procedure. In this I am using two cursors. After that I am checking if any error occurs with IF(@@Error=0). But If any error is getting or not therefore also it's going into,IF(@@Error=0)BEGIN RETURN 0ENDI want if any error occurs then only go to ELSE part other wise return 0. Can anybody please tell me what's wrong I am doing in SP ?ALTER PROCEDURE [dbo].[pr_lcr_norm] @rtshtid varchar(36)ASSET IMPLICIT_TRANSACTIONS OFF/* DECLARATIONS */DECLARE @worksheetid varchar(36);DECLARE wrkshtcursor CURSOR LOCAL READ_ONLY FORWHILE @@FETCH_STATUS=0BEGIN DECLARE nrscursor CURSOR LOCAL READ_ONLY FOR WHILE @@FETCH_STATUS=0 BEGIN Print 'Some calculations is running.' END CLOSE nrscursor; DEALLOCATE nrscursor;CLOSE wrkshtcursor;DEALLOCATE wrkshtcursor;ENDIF(@@Error=0)BEGIN RETURN 0ENDELSEBEGIN print 'Error at ' + convert(varchar,getdate(),9);ENDThanks and Regard'sHarish Patil |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 01:51:43
|
| are you using sql 2005 or above? if yes, use TRY...CATCH instead. Otherwise you have to capture @@ERROR after each operation for which you want to check error status in variables and then check if finally under IF as @@ERROr will be reset after each sql statement------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2011-11-18 : 01:51:56
|
| Hello Friend,After closing cursors, I am using following code for checking If any error occurs. If no error occurs it will return 0 otherwise it should return errror.My confusion is I think i am putting following code on wrong place because If any error occurs while executing cursor that time is going in If statement if not therefore also it'd going inside IF statement. I want If any error occurs while executing cursor then It should go in ELSE part.IF(@@Error=0)BEGINRETURN 0ENDELSEBEGINprint 'Error at ' + convert(varchar,getdate(),9);END Thanks and Regard'sHarish Patil |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 02:02:16
|
quote: Originally posted by hspatil31 Hello Friend,After closing cursors, I am using following code for checking If any error occurs. If no error occurs it will return 0 otherwise it should return errror.My confusion is I think i am putting following code on wrong place because If any error occurs while executing cursor that time is going in If statement if not therefore also it'd going inside IF statement. I want If any error occurs while executing cursor then It should go in ELSE part.IF(@@Error=0)BEGINRETURN 0ENDELSEBEGINprint 'Error at ' + convert(varchar,getdate(),9);END Thanks and Regard'sHarish Patil
You wont get error value unless you capture it just after the staement which threw error. Thats why i suggested you to use TRY...CATCH------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-11-18 : 05:31:16
|
| They don't need a cursor but if it's already coded the immediate issue is how to handle errors.That would be a try catch block by preference or testing @@error immediately after each statement - not once at the end.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-18 : 10:31:24
|
quote: Originally posted by nigelrivett They don't need a cursor but if it's already coded the immediate issue is how to handle errors.That would be a try catch block by preference or testing @@error immediately after each statement - not once at the end.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
exactlyI also thought that would be the best thing that can be done in first level.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|