How to escape square brackets inside square brackets for field name

后端 未结 4 1839
情书的邮戳
情书的邮戳 2020-12-06 16:40

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[fri         


        
相关标签:
4条回答
  • 2020-12-06 17:03

    Hang a ] on the end of inline []

       SELECT [Eggs[scrambled]] or Bacon[fried]]] FROM Yummy
    
    0 讨论(0)
  • 2020-12-06 17:21

    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.

    0 讨论(0)
  • 2020-12-06 17:24

    In an SSIS SQL statement querying an excel sheet you must substitute square brackets for parenthesis to select individual columns. For example to select "My Column [Great Column]" you would SELECT [My Column (Great Column)] FROM [$A1:AX10000]

    0 讨论(0)
  • 2020-12-06 17:25

    SET QUOTED_IDENTIFIER is ON by default so no need to escape anything

    SELECT 1 AS "[Eggs[scrambled] or Bacon[fried]]"
    
    0 讨论(0)
提交回复
热议问题