简体   繁体   English

为什么人们在 T-SQL 中使用 RaiseError 而不是 Print

[英]Why do people use RaiseError instead of Print in T-SQL

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.但是我仍然看到在许多存储过程中使用 RaiseError 而不是打印。

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:查询 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)两者都只在 6 秒后给出所需的输出(我在 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 . RAISERROR优于PRINT的优点是您可以在消息中嵌入变量值,而不必费心CASTCONVERT 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 ,您必须执行以下操作:

PRINT 'DELAY ' + CONVERT(VARCHAR(5), @m) + ' HAS ENDED'

...which is possible but cumbersome... ...这是可能的,但很麻烦...

I have the same server version as you did, and Query 2 prints the first result after 3 seconds (as expected).我有和你一样的服务器版本,查询 2 在 3 秒后打印第一个结果(如预期的那样)。 You have to switch to the Message tab on your own in Microsoft SQL Server Management Studio to see the message.您必须在 Microsoft SQL Server Management Studio 中自行切换到“消息”选项卡才能看到该消息。

We use RAISERROR to track actual errors a bit easier.我们使用 RAISERROR 来更轻松地跟踪实际错误。 This comes in handy since you can set severity for items and you can see the results right away in the Messages section.这很方便,因为您可以设置项目的严重性,并且您可以立即在“消息”部分看到结果。

PRINT is used for a really cheap/quick debugger in a multi-step process that shows how far we actually get when I don't actually expect immediate feedback. PRINT 用于多步骤过程中非常便宜/快速的调试器,它显示了当我实际上并不期望立即获得反馈时我们实际获得了多远。

There is a return statement buffer that causes PRINT statements to only flush when full (or SQL Server deigns to do so).有一个返回语句缓冲区导致PRINT语句仅在满时刷新(或 SQL Server 设计这样做)。 RAISERROR(x, y, z) WITH NOWAIT either does not use that buffer or appears to force a flush of that buffer after adding its message. RAISERROR(x, y, z) WITH NOWAIT要么不使用该缓冲区,要么在添加其消息后似乎强制刷新该缓冲区。

Other than this and the embedded variable behavior mentioned by Philippe in his answer, there appears to be no real functional difference between low-severity RAISERROR and PRINT .除了这个和 Philippe 在他的回答中提到的嵌入式变量行为之外,低严重性RAISERRORPRINT之间似乎没有真正的功能差异。

Here's a short 2018 post by Louis Davidson (@drsql) with more code samples and links to other posts with even more detail on this: https://www.red-gate.com/simple-talk/blogs/outputting-status-t-sql-code/ .这是 Louis Davidson (@drsql) 的 2018 年简短帖子,其中包含更多代码示例和其他帖子的链接,其中包含更多详细信息: https ://www.red-gate.com/simple-talk/blogs/outputting-status- t-sql-代码/

As for me, I now use RAISERROR(@msg, 0, 42) WITH NOWAIT for my batch script progress outputs after getting frustrated waiting for PRINT statements to come through.至于我,在等待PRINT语句通过后,我现在使用RAISERROR(@msg, 0, 42) WITH NOWAIT作为批处理脚本进度输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM