SQL only a throw inside if statement SQL only a throw inside if statement sql-server sql-server

SQL only a throw inside if statement


The syntax error is showing up because the previous statement hasn't been terminated. The other answers will work, but in order to do it this way you can either throw a semicolon right before the THROW, or get in the habit of terminating all statements with semicolons.

IF (@val is null)BEGIN    ;THROW 50001, 'Custom text', 1END

or

IF (@val is null)BEGIN;    THROW 50001, 'Custom text', 1;END;

You may have noticed that:

IF (@val is null)    THROW 50001, 'Custom text', 1

... will also work, and this is because SQL Server knows that the next thing to come after an IF statement is always a new T-SQL statement.

It is perhaps worth noting that Microsoft has stated that the T-SQL language in the future will require semicolons after each statement, so my recommendation would be to start building the habit now.


If this is for SQL Server, the intellisense syntax highlighter doesn't like it, but the code should compile and run fine. Of course, with it being a single statement, you don't need the BEGIN...END block at all:

IF (@val is null) THROW 50001, 'Custom text', 1


DECLARE @val NVARCHAR(50) = NULLIF @val is null    RAISERROR('Custom text', 16,16)

for different levelcheck

http://msdn.microsoft.com/en-us/library/ms164086.aspx