Regular expression to extract SQL query

后端 未结 5 894
青春惊慌失措
青春惊慌失措 2020-12-18 08:19

Is there a regex which extracts SQL queries from a string? I\'m NOT interested to validate any SQL syntax, rather and only extracting a selection o

5条回答
  •  失恋的感觉
    2020-12-18 08:31

    SQL is complicated enough that you will need context to find all statements, meaning that you can't do this with a regular expression.

    For example:

    SELECT Model FROM Product
    WHERE ManufacturerID IN (SELECT ManufacturerID FROM Manufacturer 
    WHERE Manufacturer = 'Dell')
    

    (example comes from http://www.sql-tutorial.com/sql-nested-queries-sql-tutorial/). Nested queries can be nested multiple times, start with different values, etc. If you could write a regular expression for the subset you are interested in, it would be unreadable.

    ANTLR has a SQL 2003 grammar available (I haven't tried it).

提交回复
热议问题