While I am writing SQL code, I often use print command to send message to the client application for information. One thing I noticed that this command could not return the message to client immediately due to performance concern over the network. Please see the code below.
declare @i int
select @i = 0
while @i<5
begin
print @i
select @i = @i + 1
waitfor delay '00:00:01'
end
In this example, the code will print 0, 1, 2, 3, 4, 5, five numbers in the result but you will not see every one of it every second instead, 5 numbers shows together after script finishes. While debugging T-SQL application, you may want to get the message back right after the print especially when you want to feel how quick between the print. Using raiserror can satisfy this requirement. See code below.
declare @i int
select @i = 0
while @i<1000
begin
raiserror('%d', 0,1,@i) with NOWAIT
select @i = @i + 1
waitfor delay '00:00:01'
end
this code has been tested on both SQL Server 2000 and SQL Server 2008.
1 comments:
Very useful tricks! Super cool!
Post a Comment