Handle SQL Server T-SQL¶
SQL Server's full T-SQL — variables, control-of-flow, MERGE, OUTPUT, OPENJSON, security policies — is parsed by the MSSQL vendor inside TGSqlParser.
Quick example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
What gets parsed¶
The MSSQL vendor handles:
- DML — SELECT (incl.
TOP,FOR JSON,FOR XML), INSERT, UPDATE, DELETE, MERGE - DDL — CREATE/ALTER/DROP TABLE/VIEW/PROCEDURE/FUNCTION/INDEX/TRIGGER/SCHEMA/SEQUENCE
- Procedural T-SQL —
DECLARE,SET,IF/ELSE,WHILE,BEGIN/END,BEGIN TRY/CATCH,RAISERROR,THROW,WAITFOR,GOTO, labels - Cursors —
DECLARE CURSOR(withSTATIC,LOCAL,FORWARD_ONLYetc. options),OPEN,FETCH,CLOSE,DEALLOCATE - Security —
GRANT,REVOKE,DENY;CREATE/ALTER/DROP SECURITY POLICY - Execution —
EXECUTE,EXEC,sp_executesql, batch-numbered execute (module ;1) - JSON —
OPENJSON,FOR JSON PATH/AUTO,INCLUDE_NULL_VALUES,JSON_VALUE, etc. - Batch separators —
GO(with optional repeat count)
Statement types¶
The high-level statement classes live under gudusoft.gsqlparser.stmt.mssql:
| Construct | Class |
|---|---|
CREATE PROCEDURE body |
TMssqlCreateProcedure |
EXEC / EXECUTE |
TMssqlExecute |
BEGIN..END block |
TMssqlBlock |
| MERGE | TMergeSqlStatement |
| Cursor declarations | TMssqlDeclareCursor |
Cast parser.sqlstatements.get(i) to the matching type to access T-SQL-specific properties.
Sybase ASE¶
Sybase ASE shares grammar ancestry with MSSQL. Use EDbVendor.dbvsybase for ASE-specific syntax (raiserror, sp_* extensions, (...) around CREATE PROC parameters).
Common pitfalls¶
- Smart quotes: T-SQL accepts U+2019 (
') inside string literals on some collations. The parser handles this transparently. - Comma-vs-PIVOT in SELECT: prefer parenthesised subqueries when mixing PIVOT with regular tables; T-SQL grammar is ambiguous in this corner.
GOis a batch separator, not a statement —parser.sqlstatementswill contain the statements between successiveGOs as separate top-level items.