I have tried both the below queries and they perform in same time. But still I have seen RaiseError used in many stored procedures instead of print.
Query 1:
BEGIN
WAITFOR DELAY '00:00:03.00'
PRINT 'DELAY 1 HAS ENDED'
WAITFOR DELAY '00:00:03.00'
PRINT 'DELAY 2 HAS ENDED'
END
Query 2:
BEGIN
WAITFOR DELAY '00:00:03.00'
RAISERROR ('DELAY 1 HAS ENDED', 10,1) WITH NOWAIT
WAITFOR DELAY '00:00:03.00'
RAISERROR ('DELAY 2 HAS ENDED', 10,1) WITH NOWAIT
END
Both give the desired output only after 6 seconds (I have checked this in SQL Server 2008 R2)
The advantage of RAISERROR
over PRINT
is that you may embed variables values within the message without having to bother with CAST
and CONVERT
.
For instance:
BEGIN
DECLARE @m INT = 0
WAITFOR DELAY '00:00:01.00'
SET @m += 1;
RAISERROR ('DELAY %d HAS ENDED', 10, 1, @m)
WAITFOR DELAY '00:00:01.00'
SET @m += 1;
RAISERROR ('DELAY %d HAS ENDED', 10, 1, @m)
END
This will produce same output in both examples above, except it will insert the value of variable into the message. With a PRINT
you have to do:
PRINT 'DELAY ' + CONVERT(VARCHAR(5), @m) + ' HAS ENDED'
...which is possible but cumbersome...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With