The following code will be infinite. However, after the request was completed in SSMS, the table was empty. Changing if @id is not null to if @@rowcount > 0 will produce the expected result. Why didn't @id get a null value when #t empty?
select 100 id into
(1 row (s) affected)
100
Msg 50,000, Level 16, State 10, Line 10
Test error
(1 row (s) affected) --------- The row was deleted here.
100
Msg 50,000, Level 16, State 10, Line 10
Test error
(0 row (s) affected)
100
Msg 50,000, Level 16, State 10, Line 10
Test error
......
Update:
Change select top 1 @id = Id from #t to set @id = (select top 1 Id from #t) fine.
source share