因为这if @i=0
将其设置为0,甚至打印语句将其设置为0
现在运行这个
declare @i int, @RowNum int
set @i=0
while @i<2
begin
if @i=0
begin
execute StoredProcedure @i --containing a big select
set @[email protected]@rowcount
end
else
execute StoredProcedure @i
set @[email protected]+1
end
print @RowNum
这里是另一个例子
select 1
union all
select 2
select @@rowcount --2
go
现在这将是0
select 1
union all
select 2
if 1=1
select @@rowcount --0
PRINT也打乱它,这将是2
select 1
union all
select 2
select @@rowcount --2
go
这将是0
select 1
union all
select 2
print '1'
select @@rowcount -- 0
我创建了一个帖子举出更多的例子和解释这里:When should you store @@ROWCOUNT into a variable?
任何事后结果导致它被重置,我还加了一个打印的例子,@@错误具有相同的行为 – SQLMenace 2010-08-26 14:10:26
是否需要的行为?如果@ i = 0?影响@@ rowcount值?你看我需要第一个结果集的行号。我怎样才能做到这一点? – phoenies 2010-08-26 14:12:22
看到修改后的代码,如果你还需要超过1条语句,那么在它周围加上BEGIN END – SQLMenace 2010-08-26 14:16:51