SQL - Parsing a query - sql

I worked on a parser for arithmetic expressions.
The key there was building a syntax tree, where leaves are variables and nodes are operators.
Not I'm thinking about parsing SQL queries. Parsing simple select won't be a problem, but I'm not pretty sure about the complex queries.
Can you point me to a good reference about sql parsing. Thank you in advance!

Take a look at the SQL BNF grammars

Some codesamples:
Look at sourceforge Open SQL parser.
There was a question for sql parser library before. Look there.

I'm not sure if you know C# or .NET, but LinqToSql basically does this by building expression trees that are then executed only when the query is 'called'.

Related

A grammar for Access SQL

Does a grammar (like in EBNF or similar format) exist for MS Access SQL syntax? Like how TSQL syntax is documented with EBNF: https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql?view=sql-server-2017.
I have only been able to find find tutorials with examples, but not a full grammar.
You can find the full Access SQL reference here on MS Docs.
Note that some statements are exclusive to the SQL server compatible syntax (anything with the DECIMAL type and CHECK constraints), and this isn't properly described in the reference.
It isn't as extensive and well-written as the T-SQL stuff, but it's closest to what you're asking.

Is there an ANTLR grammar for just the where clause of an ANSI SQL query?

Is there an ANTLR grammar for just the where clause of an ANSI SQL query?
i am trying to parse the conditions in where clause and change the column in that condition to something relevant to the indexes i have, so that my performance can be improved.
i want to automate this feature, so want to parse the dynamic sql and change the where clause dynamically.
Probably not.
I assume you've found an ANTLR grammar for all of ANSI SQL.
Picking out the subset that is just the where clause shouldn't really be hard.
What you'll have a harder time with is modifying the AST and regenerating source text, which I presume you want. ANTLR parsers provide no specific help in modifying the AST other than a library for hacking at the nodes as you visit them. Regeneration of source is completely on you; it can be implemented by spitting strings as you walk the tree or better, using String templates. But what has happened now is that your interest in modifying the statement has turned into a big(?) engineering job before you can do it.
See my essay on "Life After Parsing" (from bio or by googling).
I think what you want is a program transformation system. These are tools that given a grammar, can parse source code to ASTs, and carry out transformations on the AST to achieve the effect you want. PrettyPrinters to regenerate source text are usually provided.

ANTLR making a grammar for parsing insert/update/delete SQL query

I am new to ANTLR & grammar writing. My requirement is to parse insert/update/delete SQL queries to get details like which table is being update/inserted/deleting a row, list of columns & their values etc. Yes there is a good documentation of ANTLR, but if anyone can help me with specific grammar for query parsing then it would be great help.
There are various SQL grammars on the Wiki: http://www.antlr.org/grammar/list
Beware though: they are contributed by ANTLR-users, chances are that they've not been properly tested and/or contain bugs.
But why generate an SQL parser yourself? It would probably be easier to use some exiting SQL parser. Just do a search on "SQL parser Java" (assuming you're working with Java), and you're bound to get dozens of hits.
Implementing a "decent" SQL parser is actually fairly hard. In SQL, one can write all kinds of complex statements (nested joins, ...), and people really do this, so you have to implement the "full" language. (I've seen SQL queries that cover tens of pages. Stupid, yes, but then you have to work with what you encounter).
I suggest checking out SQL2011 (the standard) as being rather comprehensive. However, that grammar may not be ANTLR friendly, so be prepared for a fair bit of work.
You also have to worry about database/vendor specific extensions to standard SQL. PL/SQL (just the SQL sublanguage part) contains lots of Oracle-specific extensions. If you are facing PL/SQL stored procedures, and you want to do that table/column tracing, you may have to do the procedural part of PL/SQL too, and that's also pretty big.

BNF notation of T-SQL

Do you know where can I get the BNF (Backus Naur Form) notation for the latest version of T-SQL from. This is the microsoft version and I can't find anything for it. I found SQL2 The revised ISO standard here also called SQL92 but it seems to lack some features of microsoft's T-SQL
Have you checked out this btw?
General Sql Parser
They have enginered the notation from the ground up....
I developed TSql grammar for ANTLR 4 in EBNF form. Check it in official grammars repository. It based on msdn description.
Currently implemented syntax:
Control of Flow (MSDN).
Cursors (MSDN).
Data manipulation language (DML):
Delete (MSDN).
Insert (MSDN).
Select (MSDN).
Update (MSDN).
Expressions (MSDN).
Predicates.
Transactions (MSDN).
And another syntax. Check grammar and test files for more detail.
I know this is an old question, but I just found this grammar file hosted on bitbucket that can be used with GOLD Parsing System.
Since you're looking for TSQL's BNF (I was too), and it doesn't really exist, this grammar is the next best thing IMO.
SQL-92 in BNF
SQL Server 2005 is based on SQL-92 with some SQL-99 features and Microsoft's T-SQL extensions. Best I have found currently.
Let me know if you find a more up to date one.....

SQL query parsing in vb.net

I already searched a lot of resources on the net for parsing. Parsing in integers, parsing in char, parsing in string. However I just can't create a program that will parse a SQL Query and do conversion
For example, MySQL to MsSQL.
Does anybody have some sample query conversion code or relavent links?
SQL conversion from one database to another is quite complicated, there are lots of things to do such as data type conversion, different syntax of functions, propriety join syntax and stored procedure is much more difficult to convert.
Here are two articles with real demo to do some SQL query conversion.
Rewrite Oracle propriety joins to ANSI SQL compliant joins.
Rewrite SQL Server proprietary joins to ANSI SQL compliant joins.
Microsoft provides some guidelines for migrating from other databases to their products. You can download documents from their site which will assist you in the necessary conversions for your queries. Migration to Microsoft SQL Server 2008. The guides are word documents that you can download.
You could use Antlr or a similar tool. There is an almost ready-to-use MySQL grammar for Antlr, see http://www.antlr.org/grammar/list
Adding a vb.net target to Antlr will not be that easy, but I suppose you'd be just fine with an existing C# backend.