Update table but error on first

Last Post 05 Dec 2007 08:22 AM by TRACEYSQL. 2 Replies.
AddThis - Bookmarking and Sharing Button
Author Messages
TRACEYSQL
New Member
New Member

--
05 Dec 2007 06:17 AM
Im updating table1 - Update did not get generated some value missing and i do not want it to to got table2

But it goes to TABLE2.

How to know if update TABLE1 was successful or not...



UPDATE TABLE1 where x = '123'

--Have error check it
IF @@ERROR <>0
BEGIN
SET @ERROR_CODE = 'UPDATE TABLE ERROR'
END

--Ony here do i want to do next update because the UPDATE TABLE1 Failed.
IF @@ERROR = 0
BEGIN
UPDATE TABLE2 where x = '123'
END
nosepicker
New Member
New Member

--
05 Dec 2007 07:42 AM
First of all, the @@ERROR will give you the error output of the most recent command. Therefore, you need to save the @@ERROR value to a local variable if you want to test it twice like you're doing. Also, it depends on what you want to test. If you want the TABLE2 update to not occur if the TABLE1 update did not update any rows, then you probably want to query the @@ROWCOUNT as well (i.e., "IF @@ROWCOUNT <> 0 ..."). Is that what you were looking for?
TRACEYSQL
New Member
New Member

--
05 Dec 2007 08:22 AM
Forgot about the @@ROWCOUNT

Wanted both so set to variables for @@ERROR and @@ROWCOUNT.

Thank you.


Acceptable Use Policy
---