#8389·sqlfluff

tsql function keyword after select makes a cycle of LT09 LT14 LT02 under specific conditions

Author: mmooleCreated Aug 22, 2026Updated Sep 14, 2026
Labelsbugrule bugt-sql

Search before asking

  • I searched the issues and found no similar issues.

What Happened

in

sql
...
  -- cross apply for finding the tags for tags
  CROSS APPLY (
    SELECT STRING_AGG(T1.NAME, ',') AS TAGS_LIST
    FROM
      OPENJSON (C.TAGS, '$')
...

the function STRING_AGG should begin in a new line. Sqlfluff does this and has rules for this, but if this is the case, another rule takes over for the next step, and after this yet another, which result in the first rule being applied, thus the cycle is complete. See animated gif for an example below in 'observed behaviour'.

Expected Behaviour

New line and indent should have been applied :

sql
  CROSS APPLY (
    SELECT 
      STRING_AGG(T1.NAME, ',') AS TAGS_LIST
    FROM
      OPENJSON (C.TAGS, '$')
      WITH

Observed Behaviour

There is a cycle of the rules LT09 LT14 LT02:

Image

How to reproduce

here is the full query:

sql
SET

  @Markdown = @Html;

SELECT
  C.CUSTOMER_ID,
  COALESCE(SHORT_TAGS.TAGS_LIST, '') AS 'Contexts',
  CASE
    WHEN O.ORDER_COUNT > 10 THEN 'large' ELSE 'small'
  END AS CUSTOMER_TYPE
FROM
  CUSTOMERS AS C
  LEFT JOIN ORDERS AS O
    ON C.CUSTOMER_ID = O.CUSTOMER_ID

  -- cross apply for finding the tags for tags
  CROSS APPLY (
    SELECT 
      STRING_AGG(T1.NAME, ',') AS TAGS_LIST
    FROM
      OPENJSON (C.TAGS, '$')
      WITH
      (GID_TAG BIGINT '$.gid') AS TAG_TABLE
      INNER JOIN THE_TAGS AS T1
        ON TAG_TABLE.GID_TAG = T1.GID
    WHERE
      T1.NAME LIKE '#%'
  ) AS SHORT_TAGS

ORDER BY O.ORDER_COUNT DESC, C.CUSTOMER_ID ASC;

Dialect

tsql

Version

sqlfluff --version
sqlfluff, version 4.3.0

Configuration

.sqlfluff is empty

Are you willing to work on and submit a PR to address the issue?

  • Yes I am willing to submit a PR!

Code of Conduct