sql raiserror

This article explores the SQL Server PRINT statements, and its alternative SQL Server RAISEERROR statements to print messages in a query.

本文探讨了SQL Server PRINT语句及其替代SQL Server RAISEERROR语句,以打印查询中的消息。

介绍 (Introduction)

Suppose you execute a query with multiple steps. We want to get a message once each step completes. It helps to track the query progress. Usually, we use the SQL PRINT statement to print corresponding messages or track the variable values while query progress.

假设您执行一个包含多个步骤的查询。 每个步骤完成后,我们希望收到一条消息。 它有助于跟踪查询进度。 通常,我们使用SQL PRINT语句在查询进度时打印相应的消息或跟踪变量值。

We also use interactions or multiple loops in a query with a while or for a loop. We can also use the SQL PRINT statement to track the iteration.

我们还会在查询中使用交互或多个循环,并使用一段时间或循环。 我们还可以使用SQL PRINT语句来跟踪迭代。

We use the SQL Server PRINT statement to return messages to the client. We specify the message as string expressions input. SQL Server returns the message to the application.

我们使用SQL Server PRINT语句将消息返回给客户端。 我们将消息指定为字符串表达式输入。 SQL Server将消息返回给应用程序。

In this article, we will explore several use cases of SQL PRINT statement, its limitations, and alternatives of SQL PRINT statements.

在本文中,我们将探讨SQL PRINT语句的几种用例,其局限性以及SQL PRINT语句的替代方法。

示例1:打印字符串SQL Server PRINT语句 (Example 1: SQL Server PRINT statement to print a string )

It is the simplest example. Execute the following query in SSMS, and it returns the following message in the output:

这是最简单的例子。 在SSMS中执行以下查询,并在输出中返回以下消息:

PRINT 'My Name is Rajendra Gupta';

In SSMS, we get the PRINT statement output in SSMS message window as shown in the above image.

在SSMS中,我们在SSMS消息窗口中获得PRINT语句输出,如上图所示。

示例2:PRINT语句以打印变量值 (Example 2: PRINT statement to print a variable value)

We can use the SQL PRINT statement to print a variable value as well. Let’s define a message in a variable and later print this message:

我们也可以使用SQL PRINT语句来打印变量值。 让我们在变量中定义一条消息,然后打印此消息:

DECLARE @Msg VARCHAR(300)= 'My Name is Rajendra Gupta';
PRINT @Msg;

示例3:SQL Server PRINT语句以打印整数值 (Example 3: SQL Server PRINT statement to print an integer value)

We can use the SQL PRINT statement to print an integer value, as shown below:

我们可以使用SQL PRINT语句来打印一个整数值,如下所示:

DECLARE @a INT = 1000
PRINT @a

We can specify only CHAR, NCHAR, VARCHAR or NVARCHAR data types in the PRINT statement. In this case, it implicitly converts an integer value to the VARCHAR data type internally.

我们只能在PRINT语句中指定CHAR,NCHAR,VARCHAR或NVARCHAR数据类型。 在这种情况下,它将在内部隐式将整数值转换为VARCHAR数据类型。

Let’s use another example with an integer variable and a string in the PRINT statement. You get an error message in data type conversion because SQL Server is trying to convert the varchar data type to integer. In data type precedence, the integer data type has high precedence than the varchar data type:

让我们使用另一个示例,在PRINT语句中使用一个整数变量和一个字符串。 在数据类型转换中,您会收到一条错误消息,因为SQL Server试图将varchar数据类型转换为整数。 在数据类型优先级中,整数数据类型的优先级高于varchar数据类型:

DECLARE @a INT = 1000
PRINT 'Your queue no is ' + @a

We explicitly convert the integer data type to varchar using the SQL CAST statement:

我们使用SQL CAST语句将整数数据类型显式转换为varchar:

DECLARE @a INT= 1000;
PRINT 'Your queue no is ' + CAST(@a AS VARCHAR(10));

We can use the SQL CONCAT function as well, and it automatically does data type conversion for us. In the following query, we get the output using the CONCAT function similar to the CAST operator:

我们也可以使用SQL CONCAT函数,它会自动为我们进行数据类型转换。 在以下查询中,我们使用类似于CAST运算符的CONCAT函数获得输出:

DECLARE @a INT= 1000;
PRINT CONCAT('Your queue no is : ',@a)

示例4:带有XML类型变量值SQL Server PRINT语句 (Example 4: SQL Server PRINT statement with XML type variable value)

We can use XML data type as well with the PRINT statement, but it requires data conversion.

我们也可以在PRINT语句中使用XML数据类型,但是它需要数据转换。

As shown in the following output, we cannot directly use an XML variable in the PRINT statement. It gives an error message that implicit conversion from XML to nvarchar is not allowed:

如以下输出所示,我们不能在PRINT语句中直接使用XML变量。 它给出一条错误消息,不允许从XML隐式转换为nvarchar:

DECLARE @a XML = '<CustomerID="1" CustomerName="Rajendra"/>'
PRINT @a

We can use SQL CAST or CONVERT function explicitly and get the required output:

我们可以显式使用SQL CAST或CONVERT函数并获取所需的输出:

@a XML = '<Customer id="1" Name="Rajendra"/>'
PRINT CAST(@a AS VARCHAR(100))

示例5:具有IF条件SQL Server PRINT语句 (Example 5: SQL Server PRINT Statement with IF conditions)

Let’s use the PRINT statement to print the message satisfied in the IF condition. In this example, the variable @a contains a string. The IF condition checks for the string and prints message satisfying the condition:

让我们使用PRINT语句打印在IF条件下满足的消息。 在此示例中,变量@a包含一个字符串。 IF条件检查字符串并输出满足条件的消息:

DECLARE @a VARCHAR(100)= 'Mango';
IF @a = 'Mango'PRINT N'It is a Fruit';ELSEPRINT N'It is a vegetable';
GO

示例6:具有空值的PRINT语句 (Example 6: PRINT Statement with NULL values)

We cannot print NULL in the message using the SQL PRINT statement. The following query does not return any result:

我们无法使用SQL PRINT语句在消息中打印NULL。 以下查询不返回任何结果:

Print NULL

Let’s use the following query that contains a variable with NULL values. In the PRINT statement, we use a string along with this variable, and it does not return any message. The concatenation of a string and the variable @a (NULL) that does not return any output:

让我们使用下面的查询,其中包含一个带有NULL值的变量。 在PRINT语句中,我们将字符串与该变量一起使用,并且它不返回任何消息。 字符串与不返回任何输出的变量@a(NULL)的串联:

DECLARE @a NVarChar(100)= NULL
PRINT 'Hello' + @a

示例7:WHILE循环中SQL Server PRINT语句 (Example 7: SQL Server PRINT Statement in a WHILE loop )

As stated earlier, many times, we require knowing information about each iteration when query running in a loop such as WHILE or FOR.

如前所述,很多时候,当查询在诸如WHILE或FOR的循环中运行时,我们需要了解有关每次迭代的信息。

The following query uses WHILE loop and prints a message about each iteration:

以下查询使用WHILE循环并输出有关每次迭代的消息:

DECLARE @a INT;
SET @a = 1;
WHILE(@a < 10)BEGINPRINT CONCAT('This is Iteration no:' , @a)SET @a  = @a + 1;END;

It gives the following output. We can use a Print statement with an appropriate message and track query progress.

它给出以下输出。 我们可以将Print语句与适当的消息一起使用,并跟踪查询进度。

SQL Server PRINT语句的局限性 (Limitations of SQL Server PRINT statement)

In the previous example, we saw the use of cases of SQL PRINT statements. We have the following limitations with PRINT as well:

在前面的示例中,我们看到了SQL PRINT语句的用例。 我们对PRINT也有以下限制:

  1. We need to use CAST, CONVERT or CONCAT functions in the PRINT statement with variables 我们需要在PRINT语句中使用带有变量的CAST,CONVERT或CONCAT函数
  2. We cannot see a PRINT statement in SQL PROFILER 我们在SQL PROFILER中看不到PRINT语句
  3. The PRINT statement does not return the message immediately; it buffers the output and displays them PRINT语句不会立即返回该消息。 它缓冲输出并显示它们

Let’s elaborate point no 3 and see its alternatives.

让我们详细说明第3点,看看它的替代方案。

Execute the following query that contains two PRINT statements. Looking at the query, you might think of output in the following form:

执行以下查询,其中包含两个PRINT语句。 查看查询,您可能会想到以下形式的输出:

  • It gives the message from the first PRINT statement 它从第一个PRINT语句给出消息
  • Waits for 5 seconds 等待5秒
  • It gives the message for a second PRINT statement 它给出第二条PRINT语句的消息
  • Waits for another 5 seconds 再等5秒钟
PRINT 'My Name is Rajendra Gupta';
WAITFOR DELAY '00:00:05';
PRINT 'You are reading article on SQL PRINT statement';
WAITFOR DELAY '00:00:05';

In the following GIF image, you can note that it prints message from both SQL PRINT statements together after 10 seconds:

在下面的GIF图像中,您可以注意到10秒钟后它将同时打印来自两个SQL PRINT语句的消息:

Let’s use another example and see PRINT statement behavior. In this, we want to print the message as soon as iteration completes:

让我们使用另一个示例,看看PRINT语句的行为。 在此,我们希望在迭代完成后立即打印消息:

DECLARE @a INT;
SET @a = 1;
WHILE(@a < 15)BEGINPRINT CONCAT('This is Iteration no:' , @a)WAITFOR DELAY '00:00:01'SET @a  = @a + 1;END;

In the output, we can note that all messages output from PRINT statements appear together once the execution completes. The loop executes 14 times and waits 1 second on each execution. Therefore, we get output after 14 seconds for all PRINT statements:

在输出中,我们可以注意到,执行完成后,PRINT语句输出的所有消息都会一起出现。 循环执行14次,每次执行等待1秒。 因此,我们在14秒后获得所有PRINT语句的输出:

SQL Server buffers messages for multiple PRINT statements and displays them together.

SQL Server缓冲多个PRINT语句的消息并一起显示。

It is not the desired output, and in any case, it might not be useful as you cannot track query progress in real-time.

它不是所需的输出,在任何情况下,由于您无法实时跟踪查询进度,因此可能无用。

SQL Server RAISERROR语句 (SQL Server RAISERROR statement)

We can use an alternative to the SQL PRINT statement that is RAISERROR.

我们可以使用SQL PRINT语句的替代方法RAISERROR。

We require a few arguments in RAISERROR statements.

我们在RAISERROR语句中需要一些参数。

  • Message – It is the message that we want to print 消息 –这是我们要打印的消息
  • Severity – It is a number between 0 and 25 and defines the severity of the messages. It treats the message differently with each severity. We will see a few examples of it in this article 严重性 –介于0到25之间的数字,用于定义消息的严重性。 每种严重性对消息的处理方式都不同。 我们将在本文中看到一些示例
  • State – It is a number between 0 and 255 to distinguish one error from another. It is good to use value 1 for this article 状态 –区分一个错误和另一个错误的数字,介于0到255之间。 最好在本文中使用值1

We need to use RAISERROR statement with NOWAIT clause; otherwise, it shows the same behavior as of SQL PRINT statement:

我们需要在NOWAIT子句中使用RAISERROR语句; 否则,它显示与SQL PRINT语句相同的行为:

RAISERROR('My Name is Rajendra Gupta', 0, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';
RAISERROR('You are reading article on SQL PRINT statement', 0, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';

In the following output with SQL Server RAISERROR statements, we can note the following:

在带有SQL Server RAISERROR语句的以下输出中,我们可以注意到以下内容:

  • It gives the message from the first PRINT statement 它从第一个PRINT语句给出消息
  • Waits for 5 seconds 等待5秒
  • It gives the message for the second PRINT statement 它给出第二条PRINT语句的消息
  • Waits for another 5 seconds 再等5秒钟

Previously we used severity 0 in the RAISERROR statement. Let’s use the same query with severity 16:

以前,我们在RAISERROR语句中使用了严重性0。 让我们使用严重性为16的相同查询:

RAISERROR('My Name is Rajendra Gupta', 16, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';
RAISERROR('You are reading article on SQL PRINT statement', 16, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';

In the output, we can see the message appears in red, and it shows the message as an error instead of a regular message. You also get message id, level, and state as well:

在输出中,我们可以看到消息显示为红色,并且将消息显示为错误而不是常规消息。 您还将获得消息ID,级别和状态:

Let’s execute the following query with severity 1 and severity 16. Severity 1 shows the message with additional information, but it does not show the message as an error. You can see the text color in black.

让我们以严重性1和严重性16执行以下查询。严重性1显示带有附加信息的消息,但没有将消息显示为错误。 您可以看到黑色的文本颜色。

Another SQL Server RAISERROR shows the output message as an error:

另一个SQL Server RAISERROR将输出消息显示为错误:

RAISERROR('My Name is Rajendra Gupta', 1, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';
RAISERROR('You are reading article on SQL PRINT statement', 16, 1) WITH NOWAIT;
WAITFOR DELAY '00:00:05';

We cannot use SQL Server RAISERROR directly using the variables. We get the following output that is not the desired output:

我们不能直接使用变量来使用SQL Server RAISERROR。 我们得到以下输出,而不是所需的输出:

DECLARE @a INT;
SET @a = 1;
WHILE(@a < 15)BEGINRAISERROR('This is Iteration no:', @A, 0, 1) WITH NOWAIT;SET @a = @a + 1;END;

It prints the message but does not show the value of the variable:

它显示消息,但不显示变量的值:

We need to use the C-style print statements with RAISERROR. The following query shows the variable with the RAISERROR. You can notice that we use %s and %d to print a string and integer value:

我们需要将C样式的打印语句与RAISERROR一起使用。 以下查询显示带有RAISERROR的变量。 您会注意到我们使用%s和%d来打印字符串和整数值:

DECLARE @a INT;
SET @a = 1;
DECLARE @S VARCHAR(100)= 'This is iteration no';
WHILE(@a < 5)BEGINRAISERROR('%s:%d', 0, 1, @s, @a) WITH NOWAIT;SET @a = @a + 1;END;

We get the instant output in SQL Server RAISERROR along with WITH NOWAIT statement as per our requirement and does not use buffer to display output once the query finishes:

根据我们的要求,我们在SQL Server RAISERROR中获得即时输出以及WITH NOWAIT语句,并且在查询完成后不使用缓冲区来显示输出:

You might confuse between RAISERROR statement that it is for raising error messages in SQL Server. We can use it as an alternative to the SQL PRINT statement as well. Usually, developers use PRINT statements only to gives messages in a query. You should explore RAISERROR statements for your queries, stored procedures.

您可能会在RAISERROR语句之间混淆,该语句是用于引发SQL Server中的错误消息。 我们也可以将其用作SQL PRINT语句的替代方法。 通常,开发人员仅使用PRINT语句在查询中提供消息。 您应该探索RAISERROR语句以查询查询,存储过程。

结论 (Conclusion)

In this article, we explored the SQL Server PRINT statement and its usages to track useful milestones in a query. We also learned the limitations of it along with alternative solution RAISERROR statement in SQL Server. SQL Server RAISERROR gives you a great advantage to control output buffer behavior.

在本文中,我们探讨了SQL Server PRINT语句及其用法,以跟踪查询中的有用里程碑。 我们还了解了它的局限性以及SQL Server中的替代解决方案RAISERROR语句。 SQL Server RAISERROR为您提供了控制输出缓冲区行为的巨大优势。

翻译自: https://www.sqlshack.com/sql-server-print-and-sql-server-raiserror-statements/

sql raiserror

sql raiserror_SQL Server PRINT和SQL Server RAISERROR语句相关推荐

  1. sql server锁异常_SQL Server中异常处理的背景

    sql server锁异常 RAISERROR statement. RAISERROR语句. Figure 1 demonstrates a combination of mandatory par ...

  2. 如何在SQL Server中创建SQL依赖关系图

    Deleting or changing objects may affect other database objects like views or procedures that depends ...

  3. sql crud_使用适用于SQL Server的Python SQL库执行CRUD操作

    sql crud This article covers how to connect a Python application to Microsoft SQL Server using a 3rd ...

  4. sql concat函数_使用SQL Plus(+)和SQL CONCAT函数SQL Server CONCATENATE操作

    sql concat函数 This article explores SQL Server Concatenate operations using the SQL Plus (+) operator ...

  5. 如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件

    如果不使用 SQL Mail,如何在 SQL Server 中发送电子邮件 察看本文应用于的产品 文章编号 : 312839 最后修改 : 2006年12月21日 修订 : 10.1 本页 概要 SQ ...

  6. oracle如何调试sql,调试oracle与调试sql server存储过程

    [IT168 技术]关于存储过程的调试,知道方法以后很简单,但在不知道的时候,为了测试一个存储过程的正性,print,插入临时表等可谓是使出了浑身解数,烦不胜烦.下面就把我工作中调试oracle存储过 ...

  7. 用sql改oracle表结构,SQL server 表结构转Oracle SQL脚本

    SQL server 表结构转Oracle SQL脚本 /****** Object: StoredProcedure [dbo].[getOracle] Script Date: 2019/7/25 ...

  8. SQL server 常见的SQL语句总结(最全的)

    ---------------------------------日常学习笔记记录--------------------------------- 总结了大量的SQL语句,没有你不会的,只有你不知道 ...

  9. linux sql server客户端,Linux的MS SQL Server客户端SQuirrel SQL Client

    因为工作需要,要在ubuntu下连接mssql数据库,找了很久终于找到了SQuirreL SQL Client. SQuirreL SQL Client最大的魅力在于: 基于Java,具备良好的夸平台 ...

最新文章

  1. 如何构建顶部导航条_如何构建导航栏
  2. Saltstack 介绍、安装、配置(一)
  3. 触发器 :new和 :old的使用
  4. Redis的缓存穿透问题和雪崩问题?
  5. 用C++实现网络编程---抓取网络数据包的实现方法
  6. 2013/2/CSS文字的着重显示
  7. retrofitWrap
  8. jar类型文件图标不显示_显示的JAR文件
  9. 树莓派c语言读取dht11,树莓派直接读取 DHT11 温湿度的方法
  10. qlv格式转换成mp4 视频格式转换
  11. ISO20001文件清单
  12. 程序员面试注意几点就够了
  13. React 调和(Reconciler)原理理解
  14. 企业邮箱使用安全注意事项
  15. html5代码好学吗,0基础能学习Html5吗?Html5好学吗?
  16. win10计算机入门使用教程,win10使用教程_win10系统新手教程
  17. 计算机台式机快捷键开机,电脑快捷键如何快速开关机
  18. java处理docx文件
  19. 徕卡DNA03水准仪USB驱动
  20. ALIENTEK MINIS TM32 给定程序keil5大量错误问题办法

热门文章

  1. DOM学习之路--Mr.Ember
  2. Java实例---flappy-bird实例[最终版]
  3. 2.栅格的类中同时设置col-md-* col-sm-*的作用
  4. ASP.NET AjaxPro的应用 .AjaxPro使用中“XXX未定义”的一种解决方法(转载的)
  5. asp.net gridview 无法更新数据
  6. Linux---管道通信的使用
  7. 用js代码判断质数_在 JS 中判断,并不是都要用 ===
  8. cpu为何有两个温度?
  9. 摇一摇事件封装了设备的运动传感器
  10. 利息高的网络贷款可以不还吗?