sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

Square brackets around SQL SERVER column outside of subquery faile

Open christopherpickering opened this issue 3 years ago • 0 comments

When using square brackets around a column name that comes from a sub query in SQL SERVER we get a parsing error. If the square brackets are removed, the query passes.

For example, this query fails sql metadata, but if you remove the [ ] around b.[id], the query passes. It is a valid query both ways in SQL SERVER.

SELECT
  DISTINCT B.id,
  STUFF(
    (
      SELECT
        ', ' + A.[name]
      FROM
        (
          select
            table_enc.id,
            name
          from
            table_enc
          where
            column = 'Y'
        ) A
      WHERE
        A.id = B.[id] FOR XML PATH('')
    ),
    1,
    1,
    ''
  ) As [output]
FROM
  (
    select
      table_enc.id,
    from
      table_enc
    where
      column = 'Y'
  ) B

christopherpickering avatar Nov 16 '21 13:11 christopherpickering