I want to parse SQL code using C#.
Specifically, is there any freely available parser which can parse SQL code and generate a tree or any other structure out of it? It should also generate the proper tree for nested structures.
It should also return which kind of statement the node of this tree represents.
For example, if the node contains a loop condition then it should return that this is a "loop type" of a node.
Or is there any way by which I can parse the code in C# and generate a tree of the type I want?
Use Microsoft Entity Framework (EF).
It has a "Entity SQL" parser which builds an expression tree,
Or something like that, check it out on MSDN.
And it's all on Ballmers tick :-)
There is also one on The Code Project, SQL Parser.
Good luck.
Specifically for Transact-SQL (Microsoft SQL Server) you can use the
Microsoft.SqlServer.Management.SqlParser.Parser
namespace available in Microsoft.SqlServer.Management.SqlParser.dll, an assembly included with SQL Server and which can be freely distributed.Here's an example method for parsing T-SQL as a string into a sequence of tokens:
Note that the
TokenInfo
class is just a simple class with the above-referenced properties.Tokens
is this enumeration:and includes constants like
TOKEN_BEGIN
,TOKEN_COMMIT
,TOKEN_EXISTS
, etc.