SqlScriptDOM icon indicating copy to clipboard operation
SqlScriptDOM copied to clipboard

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

Open IVNSTN opened this issue 1 year ago • 1 comments

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

IVNSTN avatar Feb 27 '24 16:02 IVNSTN

up

IVNSTN avatar Feb 21 '25 07:02 IVNSTN