IF @SQL IS NOT NULL
BEGIN
BEGIN TRY
EXEC sp_executesql @SQL
PRINT \'SUCCESS: \' + @SQL
END TRY
BEGIN CATCH
SET @ErrorMessage =
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.
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!
From MSDN:
The statement before the THROW statement must be followed by the semicolon (;) statement terminator.