October 16, 2010 at 7:01 am
insert x values(a)
if @@rowcount>0
update x set col=2
if @@rowcount>0
print 'success'
in above sample... if update fails is there any possibility tht @@rowcount take insert stmt value and print success..?
in tht case how can i make sure tht second @@rowcount takes only from update stmt?.
is it good to SET @@rowcount=0 after first insert?...
Pls help on this.... thanks in advance
October 16, 2010 at 7:49 am
@@RowCount ALWAYS takes the row count of the last statement executed. For the second @@rowcount, that will be either the update (if it runs) or the IF.
What do you want that second @@rowcount to reflect the row count of? The insert? The Update if it happens and 0 otherwise?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2010 at 8:12 am
GilaMonster (10/16/2010)
@@RowCount ALWAYS takes the row count of the last statement executed. For the second @@rowcount, that will be either the update (if it runs) or the IF.What do you want that second @@rowcount to reflect the row count of? The insert? The Update if it happens and 0 otherwise?
Hi...
i want that second @@rowcount to reflect the row count of the update if it happens and 0 otherwise.....
October 16, 2010 at 8:39 am
That's exactly what it will do as written.
If you want to make it clearer...
DECLARE @RowsUpdated INT
SET @RowsUpdated = 0
INSERT x VALUES (a)
IF @@rowcount>0
BEGIN
UPDATE x
SET col=2
SET @RowsUpdated = @@RowCount
END
IF @RowsUpdated>0
PRINT 'success'
p.s. An insert with values will always have a rowcount > 0 unless there was an error. For errors you should be using try ... catch
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 16, 2010 at 8:43 am
Thanks a lot Gail......
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy