Incorrect syntax near 'THROW'

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

  • Sql Query to list all views in an SQL Server 2005 database
  • T-SQL Arithmetic overflow: which column?
  • sql scripting variable default value
  • Is there any tool to see the queries run against the database?
  • Syntax error (missing operator) in query expression
  • How do you get a node that contains a child node with a given attribute value in an XML document using sql server?
  • 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.

  • How to Initialize SimpleMembershipProvider?
  • What is the equivalent of varchar(max) in Oracle?
  • Load multiple Excel files into multiple SQL Server database tables using SSIS
  • String sql is not in correct format insertion fails using vb.net?How
  • Subtract minute from DateTime in SQL Server 2005
  • SQL Server rand() aggregate
  • 2 Solutions collect form web for “Incorrect syntax near 'THROW'”

    From MSDN:

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

    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.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.