TSQL Raiserror 语法不正确,遵循 MSD 指南
TSQL RaiseError incorrect syntax, following MSDN's guidelines
MSDN 声明以下语法:
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
msg_str
需要最多 2047 个字符的字符串,但 运行 需要更长的字符串。它还具有替换参数的可能性,这 t运行 比值提供的字符数更进一步地分类消息:
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 服务器(从 2008 年开始)”,截至撰写本文时,文章已更新 October 19, 2016
,其中SQL 服务器版本发布几个月后我 运行ning.
这是怎么回事?
文档:
MSDN states the following syntax:
RAISERROR
您的命令:
RAISEERROR('Test', 20, 1);
我总是犯这个错误。该命令不是 "Raise Error",而是 "Rais Error"。我不知道为什么,但我们坚持了下来...
MSDN 声明以下语法:
RAISERROR ( { msg_id | msg_str | @local_variable }
{ ,severity ,state }
[ ,argument [ ,...n ] ] )
[ WITH option [ ,...n ] ]
msg_str
需要最多 2047 个字符的字符串,但 运行 需要更长的字符串。它还具有替换参数的可能性,这 t运行 比值提供的字符数更进一步地分类消息:
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 服务器(从 2008 年开始)”,截至撰写本文时,文章已更新 October 19, 2016
,其中SQL 服务器版本发布几个月后我 运行ning.
这是怎么回事?
文档:
MSDN states the following syntax:
RAISERROR
您的命令:
RAISEERROR('Test', 20, 1);
我总是犯这个错误。该命令不是 "Raise Error",而是 "Rais Error"。我不知道为什么,但我们坚持了下来...