Skip to content

Commit

Permalink
[SQL] add support for TSQL ALTER TABLE ... WITH CHECK and temp tables
Browse files Browse the repository at this point in the history
  • Loading branch information
keith-hall committed Oct 25, 2023
1 parent b574398 commit cc2a871
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions SQL/TSQL.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ contexts:
push:
- expect-user-name

alter-table-args:
- meta_prepend: true
- match: \b(?i:with\s+check\b)
scope: keyword.other.ddl.tsql

target-options:
- meta_prepend: true
- include: with-table-options
Expand Down Expand Up @@ -1056,6 +1061,8 @@ contexts:
inside-simple-identifier-part:
- meta_prepend: true
- include: variables
- match: \#
scope: punctuation.definition.variable.tsql

###[ LITERALS ]################################################################

Expand Down
37 changes: 35 additions & 2 deletions SQL/tests/syntax/syntax_test_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@ INNER JOIN bar (NOLOCK) ON bar.Title = foo.Title COLLATE DATABASE_DEFAULT AND IS
SELECT DISTINCT *
INTO ##global_temp_table
-- ^ keyword.other.dml
-- ^^^^^^^^^^^^^^^^^^^ meta.table-name
-- ^^ meta.table-name.sql punctuation.definition.variable.tsql
-- ^^^^^^^^^^^^^^^^^ meta.table-name - punctuation
FROM some_long_table_name s
LEFT OUTER JOIN another_long_table_name (NOLOCK) a ON s.blah = a.blah AND ISNULL(p.ok, '') = ISNULL(a.ok, '') COLLATE DATABASE_DEFAULT
-- ^^^^^^^^^^^^ keyword.other.dml
Expand Down Expand Up @@ -739,7 +740,8 @@ into #temp
-- ^^^^^ meta.table-name
from @A A
-- ^ keyword.other.dml
-- ^^ meta.table-name.sql variable.other.readwrite.sql
-- ^ meta.table-name.sql variable.other.readwrite.sql punctuation.definition.variable.sql
-- ^ meta.table-name.sql variable.other.readwrite.sql
-- ^ meta.table-alias-name
inner join B ON (SELECT TOP 1 C.ID FROM C WHERE C.B LIKE B.C + '%' ORDER BY LEN(B.C) DESC) = B.ID
--^^^^^^^^ keyword.other.dml
Expand Down Expand Up @@ -2522,3 +2524,34 @@ SELECT main.id, other.another_field
FROM @table_variable AS main
CROSS JOIN some_func(@param) AS other
-- ^^^^^^^ keyword.other.dml.sql

DROP TABLE IF EXISTS #SampleTempTable;
GO
CREATE TABLE #SampleTempTable (id INT, message nvarchar(50));
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.statement.create.sql
-- ^^^ keyword.other.ddl.sql
-- ^^^^^^^^^^^^^^^^ meta.table.sql entity.name.struct.table.sql
-- ^ punctuation.definition.variable.tsql
INSERT INTO #SampleTempTable VALUES (null, 'hello');
-- ^^^^^^^^ keyword.other.dml.sql
-- ^ meta.table-name.sql punctuation.definition.variable.tsql
-- ^^^^^^^^^^^^^^^ meta.table-name.sql - punctuation
INSERT INTO #SampleTempTable VALUES (10, null);
INSERT INTO #SampleTempTable VALUES (17, 'abc');
INSERT INTO #SampleTempTable VALUES (17, 'yes');
INSERT INTO #SampleTempTable VALUES (null, null);
GO

SELECT * FROM #SampleTempTable WHERE id IS DISTINCT FROM 17;
DROP TABLE IF EXISTS #SampleTempTable;
GO

ALTER TABLE a.b WITH CHECK
-- ^^^^^^^^^^ meta.statement.alter.sql keyword.other.ddl.tsql
ADD CONSTRAINT fk_b_c
-- ^^^^^^^^^^^^^^^^^^^^^^ meta.statement.alter.sql
-- ^^^ keyword.other.ddl.sql
-- ^^^^^^^^^^ keyword.other.ddl.sql
-- ^^^^^^ meta.constraint-name.sql
FOREIGN KEY (some_id) REFERENCES a.c (some_id);
-- ^^^^^^^^^^^ meta.statement.alter.sql storage.modifier.sql

0 comments on commit cc2a871

Please sign in to comment.