TSQL RaiseError incorrect syntax, following MSDN's guidelines
Asked Answered
E

1

22

MSDN states the following syntax:

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

The msg_str expects a string up to 2047 characters but truncates longer strings. It also has the possibility of substituting parameters, which truncates the message further than the number of characters provided by the values:

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.

The severity expects a number between 0 to 25, but corrects other numbers:

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.

The state expects values 0 to 255, but corrects subzero values:

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


The issue

I get the following errors when I run these queries:

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'.


I can execute various other queries just fine. E.g.:

THROW 50001, 'Test', 1;

Msg 50001, Level 16, State 1, Line 1

Test


More info

SELECT @@VERSION produces this:

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: )

And the MSDN syntax was for "SQL Server (starting with 2008)" and as of writing, the article was updated October 19, 2016, which is months after the release of the SQL Server version I'm running.

What's going on here?

Electrolier answered 24/11, 2016 at 11:18 Comment(1)
stackoverflow.com/a/19478462 for your parameterCoppersmith
M
71

The docs:

MSDN states the following syntax:

RAISERROR

Your command:

RAISEERROR('Test', 20, 1);

I am always making this mistake. The command is not "Raise Error", but rather "Rais Error". I don't know why, but we're stuck with it...

Macao answered 24/11, 2016 at 11:42 Comment(8)
In addition; MSDN states "For severity levels from 19 through 25, the WITH LOG option is required.".Gilman
Also don't you need to pass the parameter correctly?Coppersmith
@destination-data I figure the asker can fix the severity problem themselves, once they get to it. And I'm not sure what you mean by the second comment: RAISERROR('Test', 12, 1); works fineMacao
Hi AakashM. Upvoted your fine answer. Just wanted to point out what when the severity is above 19, as in the example, you cannot use RAISERROR on its own. RAISERROR('Test', 20, 1); will fail. RAISERROR('Test', 20, 1) WITH LOG; will not.Gilman
It is RaIsError referring to to a mistake made by Ra, the Egyptian sun god. Easy enough to remember. Are you suggesting we all get together and buy a vowel for Microsoft so it can be changed to something else?Hasan
oh my god HOURS of my life on this fakking microsoftSwart
And to add insult to injury, RAISEERROR shows up in pink indicating it's a keyword. Otherwise you'd ask "why isn't SSMS recognizing this as a keyword" and would find the answer immediately.Peroneus
MS should add both RAISERROR and RAISEERROR and be done with it as a common mistake - I bet you can guess why I'm on this answer again!Inspissate

© 2022 - 2024 — McMap. All rights reserved.