能否以SQL Server同款方式解析T-SQL?其AST生成代码是否公开?
Great questions! Let’s break this down clearly for you:
Can you parse T-SQL exactly the way SQL Server does?
Absolutely—you can replicate SQL Server’s exact parsing behavior using official tools and libraries maintained by Microsoft:
- SQL Server Management Studio (SSMS): The query editor in SSMS uses SQL Server’s native parsing engine directly. When you type a query and get syntax validation errors or IntelliSense suggestions, that’s the same parser the SQL Server engine uses internally.
- Microsoft.SqlServer.TransactSql.ScriptDom: This is a free, official .NET library that implements T-SQL parsing matching SQL Server’s syntax rules perfectly. It can generate a full AST (Abstract Syntax Tree) for valid T-SQL, and it’s updated alongside new SQL Server versions to support the latest features. You can use this library in your own applications to parse, analyze, or even modify T-SQL scripts with the same accuracy as SQL Server.
Is SQL Server's internal AST-building code available to regular users?
No, the closed-source internal code that SQL Server uses to build ASTs during query compilation isn’t publicly accessible. That said, the ScriptDom library I mentioned above is Microsoft’s official, supported alternative—it’s a public-facing implementation that mirrors the internal parsing logic closely, even if it’s not the exact same code running in the SQL Server engine.
ScriptDom is the go-to tool for developers needing to work with T-SQL ASTs, and it’s easy to integrate via NuGet packages into any .NET project.
内容的提问来源于stack exchange,提问作者mark

