Requirements: You need to determine whether there are any syntax errors in the SQL statement to ensure that it can be executed normally in the SQL Server database, and then obtain the table name of the execution statement.
SqlScriptDOM
SqlScriptDOM (SQL Script Document Object Model) is a part of the .NET framework that is mainly used to parse and manipulate T-SQL scripts. It provides a set of APIs that allow developers to programmatically read, modify, or generate T-SQL code for SQL Server. This is useful in situations where database scripting needs to be automated, such as in database migration tools, database versioning, or any application where SQL statements need to be dynamically constructed.
With SqlScriptDOM, you can:
Parse SQL scripts: Convert T-SQL code into a tree structure for easy understanding and processing. Traversing AST (Abstract Syntax Tree): Access and modify different parts of your SQL script by traversing this tree structure. Generate SQL scripts: Regenerate T-SQL code based on the modified abstract syntax tree. Perform static analysis: Check for potential errors or irregularities in SQL scripts without actually running them. Support for a variety of SQL elements, including but not limited to table definitions, views, stored procedures, etc.
Source:The hyperlink login is visible. Documentation:The hyperlink login is visible.
Version mapping reference
SqlVersionFlags Quick Reference:
| Logo | SQL Server version | year | Common characteristics | | TSql80 and above | 2000 | 2000 | Basic T-SQL | | TSql90 and above | 2005 | 2005 | XML、CTE | | TSql100 and above | 2008 | 2008 | Merge, file flow | | TSql110 and above | 2012 | 2012 | sequences, window functions | | TSql120 and above | 2014 | 2014 | Memory OLTP, maximum duration | | TSql130 and above | 2016 | 2016 | JSON, tense table | | TSql140 and above | 2017 | 2017 | Figure, STRING_AGG | | TSql150 and above | 2019 | 2019 | UTF-8, Smart QP | | TSql160 and above | 2022 | 2022 | Recoverable constraints, JSON improvements | | TSql170 and above | 2025 | 2025 | Vector search, AI features |
NUGET references as follows:
Parse table names
Parse the table name in the select statement with the following code:
As shown below:
Verify SQL query security, only allow execution of SELECT class (untested), the code is as follows:
(End) |