October 12, 2010 at 2:09 am
whts the scope of @@rowcount
for ex
begin
insert stmt
if @@rowcount>0
begin
insert stmt
if @@rowcount>0-- will it be affected by the first insert statement?
do smthg
end
end
any help?
October 12, 2010 at 2:12 am
Yes. As in Yes it will get effected.
/T
October 12, 2010 at 5:26 am
but that shouldnt affect from first @@rowcount value..right?.. is thr any other way to make sure a rows are inserted or updated?
October 12, 2010 at 5:32 am
every insert/update/select/set/delete statement resets the @@rowcount to the rows affected by the executed statement, so it's a best practice to use a variable to capture the rowcount you need,:
begin
declare @myrowcount int,
@myrowcount2 int
insert stmt
SET @myrowcount=@@rowcount
if @myrowcount>0
begin
insert stmt
SET @myrowcount2 =@@rowcount
if @myrowcount2 >0-- will it be affected by the first insert statement?
do smthg
end
end
Lowell
October 12, 2010 at 5:55 am
Thanks Lowell!!!!
October 12, 2010 at 6:28 am
Lowell (10/12/2010)
every insert/update/select/set/delete statement resets the @@rowcount to the rows affected by the executed statement
It's more general than that. It's just about every statement.
SELECT * FROM master.sys.objects -- 74 rows
IF (1=1)
SELECT @@ROWCOUNT AS RowsAffected -- 0, because the IF did not affect any rows
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 12, 2010 at 6:35 am
GilaMonster (10/12/2010)
It's more general than that. It's just about every statement.
SELECT * FROM master.sys.objects -- 74 rows
IF (1=1)
SELECT @@ROWCOUNT AS RowsAffected -- 0, because the IF did not affect any rows
wow i did not know that even an IF statement affects it! thanks once again Gail!
Lowell
October 12, 2010 at 6:59 am
Lowell (10/12/2010)
GilaMonster (10/12/2010)
It's more general than that. It's just about every statement.
SELECT * FROM master.sys.objects -- 74 rows
IF (1=1)
SELECT @@ROWCOUNT AS RowsAffected -- 0, because the IF did not affect any rows
wow i did not know that even an IF statement affects it! thanks once again Gail!
I found that out after using this construct and getting really, really confused...
Insert <stuff>
IF @@Error = 0
SET @RowCount = @@RowCount
ELSE
GOTO ErrorHandler
@@Error behaves the same way.
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
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply