如何将方括号内的方括号中的方括号用于字段名称

编程入门 行业动态 更新时间:2024-10-12 01:22:29
本文介绍了如何将方括号内的方括号中的方括号用于字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一些生成列名的动态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.

更多推荐

如何将方括号内的方括号中的方括号用于字段名称

本文发布于:2023-10-24 20:58:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方括号   字段   如何将   括号内   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!