SQL only a throw inside if statement
Asked Answered
D

3

44

I am adding some validation to a couple of stored procedures and need to check if some of the variables are not null (they are populated earlier in the stored procedure).

I have been trying to add a "throw" inside an if statement like below:

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

This causes a syntax error on the "throw" as it is looking for other code inside the if statement prior to the throw but I only need it to perform the throw inside the if statement.

I need to keep the stored procedure as light as possible to keep it as fast as possible to execute.

Does anyone have any ideas?

Diabolism answered 5/8, 2013 at 8:0 Comment(1)
THROW is a SQL Server 2012 featureRior
G
81

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', 1
END

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.

Gilchrist answered 13/6, 2015 at 20:24 Comment(5)
This should be the answer.Tedra
That syntax surely looks weird BEGIN; statements here END; Why is it OK to terminate the BEGIN !?Pipeline
@Pipeline Blame Microsoft maybe? :-) Perhaps when the throw is the only statement, omitting BEGIN/END is preferable, and when some other statement is used before the throw, the semicolon after BEGIN is unnecessary.Gilchrist
Similar to the WITH statement to create a Common Table Expression : the previous statement also needs to be terminated for WITH to work :PDemean
Apparently, it thinks END TRY BEGIN CATCH is a single statement. It won't allow a semicolon after END TRY, and displays an error "expecting END CATCH". But you can terminate the whole thing as END TRY BEGIN CATCH;Graffito
M
5

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
Maidenhair answered 5/8, 2013 at 8:19 Comment(1)
I still get "incorrect syntax near throw" when building with readyroll (it compiles and works correct in ssms.Diabolism
T
-2
DECLARE @val NVARCHAR(50) = NULL
IF @val is null

    RAISERROR('Custom text', 16,16)

for different level check

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

Tia answered 5/8, 2013 at 8:36 Comment(3)
Looks like the readyroll software does not like throw and gives an error when compiling but using RAISERROR works correctly.Diabolism
RAISERROR should no longer be used in new code, since it will be discontinued in the future.Gilchrist
@Gilchrist Its 2019 ending but still we are using RAISERRORTubing

© 2022 - 2024 — McMap. All rights reserved.