axl4u82 has asked for the wisdom of the Perl Monks concerning the following question:

Is there any full blown code to parse complex SQL queries from text files readily available? By complex I mean it should be able to identify table and column aliases and the various joins used. Any help would be appreciated.

Replies are listed 'Best First'.
Re: SQL Parsing
by Corion (Patriarch) on Feb 24, 2006 at 08:06 UTC

    Did you look at SQL::Statement? SQL::Statement::Structure documents how it parses SQL statements. I guess you might have to hack it a bit so it accepts your specific SQL database, but for relatively standard SQL queries, it should work.

      SQL::Statement doesn't recognise table aliases, case statements and parameters i guess.
        You're correct that it doesn't recognize case statements. It does recognize table aliases. I'm not sure what you mean by paramaters.
Re: SQL Parsing
by arkturuz (Curate) on Feb 24, 2006 at 09:16 UTC
    There was a similar node before. Also look at this.
      I have seen that already but am not sure if i can use the same lexing technique for table name aliases. Also, How do i handle parameterised table names?
Re: SQL Parsing
by pboin (Deacon) on Feb 24, 2006 at 13:04 UTC

    Would you care to expand on *why* you want to do this? Your question is short on detail.

    To a certain extent, you're running pretty close to the old "only perl can parse Perl" example. The only thing that's likely to reliably and perfectly parse the more advanced and obscure bits of your particular dialect of SQL would be the SQL engine from your DB of choice.

    I just have a feeling that if you explain more, some monk might come up with a creative way to help you that you might not expect...

      The situation here is like this... The SQL generated by a set of reports need to be parsed in order to obtain the metadata. The reports are hitting a database of type 'A'.The metadata obtained after parsing the SQL will be used to identify the table structures that need to be created in a database of type 'B' so that the reports can be migrated to 'B'. The only good thing is that both 'A' and 'B' support SQL. The reports are my only source of information about 'A'