microsoft / SqlScriptDOM

ScriptDOM/SqlDOM is a .NET library for parsing T-SQL statements and interacting with its abstract syntax tree
MIT License
128 stars 13 forks source link

ScriptDom parses table-level constraint after the last column as column-level constraint if not preceded by comma separator #72

Open IVNSTN opened 4 months ago

IVNSTN commented 4 months ago

In this code sample:

CREATE TABLE dbo.foo
(
    bar INT NOT NULL DEFAULT 0
    , far VARCHAR(100) NOT NULL
    , zar DATE PRIMARY KEY (bar, far)
)

comma separator is missing before PRIMARY KEY constraint declaration. And ScriptDom parses it as a column-level constraint linked to zar column which is wrong. Note, in such case column-level constraint has columns property with different columns listed. It should be parsed as table level constraint.

The syntax brings some ambiguity however it is completely valid. For created table sp_help shows no constraint for column zar and shows table-level unnamed PRIMARY KEY constraint on bar and far columns: image

While developing rules for our custom T-SQL linter with columns declared as inline primary key involved, I have to double check if the inline PK is actually related to analyzed column. It would be great if such table-level constraints were parsed exactly as table-level constraints, not column-level.

ScriptDom version: 161.8919.0 Compatibility level used for parsing: 150