Why does concatenating strings in the argument of EXEC sometimes cause a syntax error in T-SQL?

前端 未结 1 1331
心在旅途
心在旅途 2021-01-17 11:40

In MS SQL Server Management Studio 2005, running this code

EXEC(\'SELECT * FROM employees WHERE employeeID = \' + CAST(3 AS VARCHAR))

gives

相关标签:
1条回答
  • 2021-01-17 12:03

    The documentation states that EXEC can take either a string variable, a constant T-SQL string, or combinations/concatenations of both of them.

    Your "why does this work" example uses a concatenation of a constant T-SQL string and a string variable, and so is perfectly legal.

    0 讨论(0)
提交回复
热议问题