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
当CATCH执行时,我得到以下错误:
Msg 102,Level 15,State 1,Line 257
Incorrect Syntax near ‘THROW’.
用打印@ErrorMessage替换THROW.
用文字字符串替换@ErrorMessage变量可以工作.
然而,根据文档,THROW应该能够使用变量.不知道该怎么做
解决方法
从
MSDN:
The statement before the THROW statement must be followed by the semicolon (;) statement terminator.