Bobcares

Why Incorrect Syntax Near RAISEERROR | All About

by | May 16, 2024

Let’s fix the incorrect syntax near RAISEERROR. At Bobcares, we assist our customers with several SQL Server queries on a daily basis as part of our Microsoft SQL Server Support Service.

Fixing the Incorrect Syntax near RAISEERROR

What is RAISEERROR in SQL Server?

RAISEERROR is a SQL Server statement that creates custom error messages and reports user-defined errors. We can change the error severity, state, and message phrases to provide further feedback to users or apps interacting with the database. The severity level in RAISEERROR shows the importance of the error, which ranges from 11 to 20. The syntax is as follows:

incorrect syntax near raiseerror

Severity: These are user-defined severity levels for this message. Any user can specify severity levels ranging from 0 to 18. However, severity levels 19-25 can only be defined by members of the sysadmin fixed server role or users with ALTER TRACE rights.

State: These can be defined to as an integer ranging from 0 to 255, as well as negative or greater-than-255 values due to an error. If the same issue occurs in many areas, we can use a unique state number to find which piece of code is causing the error.

Message: The text of the error message. It can be a string constant, a string variable, or a string function. This parameter is a must.

We can use RAISEERROR in stored procedures, triggers, user-defined functions, and batches. It stops the running of the current code block and reports an error to the calling program or client. We can use TRY…CATCH blocks or other methods to fix the error messages issued by RAISEERROR.

Examples

In this case, the user provides the below trigger and faced error incorrect syntax near ‘)’ line 9 which is the Raiserror string.

CREATE TRIGGER tgr_XXXX

on dbo.XXXXX

AFTER INSERT

AS

BEGIN

SELECT COUNT(*) FROM EVENTS

IF COUNT(*).=2

RAISERROR ( 50004,16,1)

Here, there is no END and also a period after count(*). So the solution offered is as follows:

CREATE TRIGGER tgr_XXXX

on dbo.XXXXX

AFTER INSERT

AS

BEGIN

SELECT COUNT(*) FROM EVENTS

IF COUNT(*)=2

RAISERROR ( 50004,16,1)

end

[Looking for a solution to another query? We are just a click away.]

Conclusion

This post explains about fixing the incorrect syntax near RAISEERROR, along with an example offered by our Experts.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.