如何将方括号内的方括号中的方括号用于字段名称 [英] How to escape square brackets inside square brackets for field name

查看:145
本文介绍了如何将方括号内的方括号中的方括号用于字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些生成列名的动态SQL。一个快速的例子可能是这样的:

  SELECT dbo.getSomething(123)[Eggs [炒]或培根] 

最后一列名称应为:




$ 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.

这篇关于如何将方括号内的方括号中的方括号用于字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆