Incorrect syntax near 'THROW'

15,714

Solution 1

From MSDN:

The statement before the THROW statement must be followed by the semicolon (;) statement terminator.

Solution 2

From the Documentation on THROW, Remarks:

The statement before the THROW statement must be followed by the semicolon (;) statement terminator.

It's a good habit to always end your statements with a semi-colon.

Solution 3

I just hit the same error but for a completely different reason. The machine I'm using is slightly old but has SSMS 2012 (the version that THROW was introduced). However the actual SQL server is 10.5 (which is 2008 R2; see https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql-compatibility-level) and so the command is not valid.

Hopefully there won't be too many instances of ten year old setups out there but double check if you get this and you're sure your syntax is correct!

Share:
15,714
Metaphor
Author by

Metaphor

Updated on June 06, 2022

Comments

  • Metaphor
    Metaphor almost 2 years
    IF @SQL IS NOT NULL
    BEGIN
        BEGIN TRY 
            EXEC sp_executesql @SQL
            PRINT 'SUCCESS: ' + @SQL
        END TRY 
        BEGIN CATCH
            SET @ErrorMessage = 
                        N'Error dropping constraint' + @CRLF
                        + 'Table ' + @TableName + @CRLF
                        + 'Script: ' + @SQL + @CRLF
                        + 'Error message: ' + ERROR_MESSAGE() + @CRLF
            THROW  50100, @ErrorMessage, 1;
        END CATCH
    END
    

    When the CATCH executes, I get the following error:

    Msg 102, Level 15, State 1, Line 257
    Incorrect syntax near 'THROW'.

    Replacing THROW with PRINT @ErrorMessage works.

    Replacing @ErrorMessage variable with a literal string works.

    According to the docs, however, THROW is supposed to be able to take a variable. Not sure what to make of this.

  • Metaphor
    Metaphor over 8 years
    I guess I'll be spelling it ;THROW from now on. Thanks!
  • Sam Axe
    Sam Axe over 8 years
    Good plan. It's really annoying when there are special rules for some keywords and not for the rest.
  • Tim Sparkles
    Tim Sparkles about 7 years
    "It's a good habit to always end your statements with a semi-colon." Sure, but it's really weird to have to terminate begin with a semicolon, given if @@rowcount = 0 begin throw ###, '', # end
  • TT.
    TT. about 7 years
    @Timbo BEGIN isn't a statement. BEGINand END are control-of-flow language keywords.
  • Tim Sparkles
    Tim Sparkles about 7 years
    Which is why I find it strange that the build wants begin to be terminated with a semicolon.
  • TT.
    TT. about 7 years
    @Timbo Indeed that is strange. Nothing's perfect I guess ;)
  • Elaskanator
    Elaskanator over 5 years
    Just a note that forgetting the semicolon and using it after a parameterless procedure call will pass parsing (in versions before 2012), and instead throw the error 8146 at runtime with the message "Procedure ParameterlessProcedure has no parameters and arguments were supplied."
  • maets
    maets over 5 years
    agree with @Timbo, I prefer writing ;THROW when throw is on a new line
  • vibs2006
    vibs2006 about 5 years
    lol... Microsoft at its best. If they want to implement semicolons as Oracle does then there should be some consistency!