sql - TSQL Raiserror 语法不正确,遵循 MSD 指南

标签 sql sql-server tsql sql-server-2016 raiserror

MSDN声明以下语法:

RAISERROR ( { msg_id | msg_str | @local_variable }  
{ ,severity ,state }  
[ ,argument [ ,...n ] ] )  
[ WITH option [ ,...n ] ] 

msg_str 期望字符串最多为 2047 个字符,但会截断更长的字符串。它还可以替换参数,这会比值提供的字符数进一步截断消息:

The error message can have a maximum of 2,047 characters. If the message contains 2,048 or more characters, only the first 2,044 are displayed and an ellipsis is added to indicate that the message has been truncated. Note that substitution parameters consume more characters than the output shows because of internal storage behavior. For example, the substitution parameter of %d with an assigned value of 2 actually produces one character in the message string but also internally takes up three additional characters of storage. This storage requirement decreases the number of available characters for message output. When msg_str is specified, RAISERROR raises an error message with an error number of 50000.

severity 需要一个介于 0 到 25 之间的数字,但会更正其他数字:

Severity levels from 0 through 18 can be specified by any user. Severity levels from 19 through 25 can only be specified by members of the sysadmin fixed server role or users with ALTER TRACE permissions. For severity levels from 19 through 25, the WITH LOG option is required. Severity levels less than 0 are interpreted as 0. Severity levels greater than 25 are interpreted as 25.

state 期望值介于 0 到 255 之间,但会更正低于零的值:

[state] is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used.

<小时/>

问题

运行这些查询时出现以下错误:

RAISEERROR('Test', 20, 1);

Msg 102, Level 15, State 1, Line 1

Incorrect syntax near 'Test'.

<小时/>
DECLARE @err_message nvarchar(255);
SET @err_message = 'Test';
RAISEERROR(@err_message, 20, 1);

Msg 102, Level 15, State 1, Line 3

Incorrect syntax near 'RAISEERROR'.

<小时/>

我可以很好地执行各种其他查询。例如:

THROW 50001, 'Test', 1;

Msg 50001, Level 16, State 1, Line 1

Test

<小时/>

更多信息

SELECT @@VERSION 产生以下内容:

Microsoft SQL Server 2016 (RTM-GDR) (KB3194716) - 13.0.1722.0 (X64) Sep 26 2016 13:17:23 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 8.1 Pro 6.3 (Build 9600: )

MSDN 语法是“SQL Server(从 2008 年开始)”,在撰写本文时,该文章已于 2016 年 10 月 19 日 更新,即几个月后我正在运行的 SQL Server 版本的版本。

这是怎么回事?

最佳答案

文档:

MSDN states the following syntax:

RAISERROR

您的命令:

RAISEERROR('Test', 20, 1);

总是犯这个错误。该命令不是“Raise Error”,而是“Rais Error”。我不知道为什么,但我们坚持下去......

关于sql - TSQL Raiserror 语法不正确,遵循 MSD 指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40785033/

相关文章:

sql - 如何在 SQL 中拆分数据

tsql - 如何转换 : YYYYMMDD to a datetime in T-SQL? 形式的 varchar 字段

sql - 在联合查询中使用计数

mysql - 按两列中的最大值排序

c# - IDENTITY_INSERT SQL 脚本

sql-server - 只替换日期部分

sql-server - 更新表中值表的 2 列

mysql - 无法在 mysql 中应用外键约束

php - 如何检查拍卖是否完成 - 触发 PHP 处理

mysql - 收集最大月份数据