Login    Forum    Search    FAQ   Blog

Board index » Server Side Development » SQL




 Page 1 of 1 [ 6 posts ] 



Author Message
 Post subject: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:41 pm 
Offline

Joined: Sat Feb 21, 2009 12:32 pm
Posts: 81
I m updatin table1 - Update didn't get generated sum value missin nd
i do not want it 2 got table2

howerv it goes 2 the TABLE2.

How 2 know if update TABLE1 was successful r not...

Code:

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
IF @@ERROR = 0
BEGIN
UPDATE TABLE2 where x = '123'
END


Top 
 Post subject: Re: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:41 pm 
Offline

Joined: Sat Feb 21, 2009 1:08 pm
Posts: 549
Instead of using 2 IF statements, use an ELSE clause:

Code:
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
ELSE IF @@ERROR = 0
BEGIN
UPDATE TABLE2 where x = '123'
END

The reason was just bcoz the @@ERROR was cleard nd
reset on each statemnt that was executed. Since ur code executd a SET
statement, da @@ERROR gets a val of 0 aftr dat .


Top 
 Post subject: Re: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:42 pm 
Offline

Joined: Sat Feb 21, 2009 12:32 pm
Posts: 81
The table1 didn't get updatd just as da condition where was not met.
So now i m assumin da error as 0 as its not an error just never got updated.
But do not want to continue...on table 2.

Code:
BEGIN
--Update table 1
Update TABLE1
IF @@ERROR <>0
BEGIN
SET @ERROR_CODE = 'TABLE ERROR'
END
ELSE IF @@ERROR = 0
BEGIN
print 'here'
UPDATE TABLE2
END


Top 
 Post subject: Re: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:42 pm 
Offline

Joined: Sat Feb 21, 2009 12:32 pm
Posts: 81
I got it i did
Code:
BEGIN
--Update table 1
Update TABLE1
set @check_rowcount = @@ROWCOUNT
IF @@ERROR <>0
BEGIN
SET @ERROR_CODE = 'TABLE ERROR'
END
ELSE IF @@ERROR = 0 AND @CHECK_ROWCOUNT > 0 (added this so if updated the rowcount be 1)
BEGIN
print 'here'
UPDATE TABLE2
END



Top 
 Post subject: Re: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:42 pm 
Offline

Joined: Sat Feb 21, 2009 11:06 am
Posts: 1794
What kind f syntax was this?

Also @@ERROR is hihgly volatile, so once it gets
referenced (as in an IF check) the val is reset.

[code]
DECLARE @ErrorCode VARCHAR(200)

UPDATE Table1
SET xxx = 'yyy'
WHERE zzz = 4

IF @@ERROR <> 0
SET @ErrorCode = 'TABLE ERROR'

IF @ErrorCode IS NULL
UPDATE Table2
SET xxx = 'yyy'
WHERE zzz = 4
[/code


Top 
 Post subject: Re: Updatin failing
 Post Posted: Tue Nov 17, 2009 12:43 pm 
Offline

Joined: Sat Feb 21, 2009 1:08 pm
Posts: 549
Thanks i do what your doing reset to a variable and check the variable ...


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 6 posts ] 




Board index » Server Side Development » SQL


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron