Home Generate regular expression to match SQL Server select statement
Reply: 0

Generate regular expression to match SQL Server select statement

Gerry
1#
Gerry Published in 2018-02-14 08:17:11Z

I need to make a regular expression to match select query syntax of SQL Server. Below is the query structure taken from msdn:

[ WITH { [ XMLNAMESPACES ,] [ <common_table_expression> [,...n] ] } ]  
    <query_expression>   
    [ ORDER BY { order_by_expression | column_position [ ASC | DESC ] }   
  [ ,...n ] ]   
    [ <FOR Clause>]   
    [ OPTION ( <query_hint> [ ,...n ] ) ]   
<query_expression> ::=   
    { <query_specification> | ( <query_expression> ) }   
    [  { UNION [ ALL ] | EXCEPT | INTERSECT }  
        <query_specification> | ( <query_expression> ) [...n ] ]   
<query_specification> ::=   
SELECT [ ALL | DISTINCT ]   
    [TOP ( expression ) [PERCENT] [ WITH TIES ] ]   
    < select_list >   
    [ INTO new_table ]   
    [ FROM { <table_source> } [ ,...n ] ]   
    [ WHERE <search_condition> ]   
    [ <GROUP BY> ]   
    [ HAVING < search_condition > ]

I need to make a regex which can match any select query written using above syntax.

I have created below regex, but it is not up to the mark. After where clause it is matching everything. https://regexr.com/3kp3g

Please help

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.338519 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO