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
Hang a ]
on the end of inline []
SELECT [Eggs[scrambled]] or Bacon[fried]]] FROM Yummy
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.
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]
SET QUOTED_IDENTIFIER is ON by default so no need to escape anything
SELECT 1 AS "[Eggs[scrambled] or Bacon[fried]]"