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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Error handling

Author  Topic 

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-05-10 : 09:17:26
I have a procedure there
i am having some update statement and some insert statement..

If any error occurs i want to complete the execution of
all the statement but want to return the integer 0 else 1 if all success.


Can anyone suggest me how to achieve this...
Please help me out...


Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-10 : 09:36:08
For each statement check the value of @@Error assign the variable accordingly

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-05-10 : 09:53:49
@@RowCount or @@Error?

--
Gail Shaw
SQL Server MVP
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-10 : 10:01:15
quote:
Originally posted by GilaMonster

@@RowCount or @@Error?

--
Gail Shaw
SQL Server MVP


It should be @@Error
I corrected it now

Thanks

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-05-11 : 02:11:35
Yes thats correct
but i have a lot of statements other than insert update commands
because error may occurs in select statement also
so do i need to check @@Error just after each statement
or any other way to achieve it...

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-05-11 : 03:23:19
You need to check @@Error after every statement.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-05-11 : 03:40:20
But in the mean time i explored something different
that if any error occured
by default its not possible to execute the next statement control will come out of the proc.
so i m surprised how the @@Error can be used
please anybody give me such example to use it.

Thanks in advance...

Vaibhav T

To walk FAST walk ALONE
To walk FAR walk TOGETHER
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-11 : 03:46:12
Make sure to read this
http://www.sommarskog.se/error-handling-I.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-05-11 : 04:59:55
quote:
Originally posted by vaibhavktiwari83

But in the mean time i explored something different
that if any error occured
by default its not possible to execute the next statement control will come out of the proc.


That's not at all true. Depends on what kind of error it is. If it's a statement-terminating error (quite common) then the statement fails and execution continues at the next statement. Control does not leave the proc.



--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -