以下代码将无限循环。但是,在 SSMS 中停止查询后,该表为空。更改if @id is not null
为if @@rowcount > 0
将得到预期的结果。为什么@id 为空时没有得到空值#t
?
select 100 id into #t
l:
declare @id int
select top 1 @id = Id from #t
if @id is not null
begin
print @id
--begin try
raiserror ('Test error', 16, 10)
--end try
--begin catch
--end catch
delete from #t where Id = @id
goto l
end
(1 行受影响) 100 消息 50000,第 16 级,状态 10,第 10 行 测试错误 (1 行受影响) --------- 该行已在此处删除。 100 消息 50000,第 16 级,状态 10,第 10 行 测试错误 (0 行受影响) 100 消息 50000,第 16 级,状态 10,第 10 行 测试错误 ……
更新:
更改select top 1 @id = Id from #t
为set @id = (select top 1 Id from #t)
正常工作。