sql-server – “THROW”附近的语法错误

前端之家收集整理的这篇文章主要介绍了sql-server – “THROW”附近的语法错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. IF @sql IS NOT NULL
  2. BEGIN
  3. BEGIN TRY
  4. EXEC sp_executesql @sql
  5. PRINT 'SUCCESS: ' + @sql
  6. END TRY
  7. BEGIN CATCH
  8. SET @ErrorMessage =
  9. N'Error dropping constraint' + @CRLF
  10. + 'Table ' + @TableName + @CRLF
  11. + 'Script: ' + @sql + @CRLF
  12. + 'Error message: ' + ERROR_MESSAGE() + @CRLF
  13. THROW 50100,@ErrorMessage,1;
  14. END CATCH
  15. 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.

猜你在找的MsSQL相关文章