因爲這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