最后一列名称应为:
$ p如果我尝试运行它,将会单词OR的错误(即使我用xyz替换它仍然是该令牌上的错误)。如果我取出内部的方括号,问题就是修复。所以我的结论是,你不能嵌套方括号,除非你以某种方式逃脱他们。
在这种情况下,我如何逃避他们?
解决方案您可以使用 quotename 函数查看正确的转义。
选择quotename('Eggs [炒]或培根[fried]')返回
[Eggs [炒]]或培根[fried]]]所有关闭方括号需要翻倍。
I have some dynamic SQL that generates column names. A quick example might be something like this:
SELECT dbo.getSomething(123) [Eggs[scrambled] or Bacon[fried]]The final column name should be this:
Eggs[scrambled] or Bacon[fried]
If I try to run this it will error on the word OR (even if I replace it with xyz it still errors on that token). The problem is fixed if I take out the inner sets of square brackets. So my conclusion is that you can't nest square brackets unless you somehow escape them.
How do I escape them in this case?
解决方案You can use the quotename function to see the proper escaping.
select quotename('Eggs[scrambled] or Bacon[fried]')Returns
[Eggs[scrambled]] or Bacon[fried]]]So all closing square brackets need to be doubled up.
更多推荐
如何将方括号内的方括号中的方括号用于字段名称
发布评论