1

Consider I have a SQL query like this:

SELECT c_mandant, 
    hist_datum, 
    parkey1, 
    parkey2, 
    funktionscode, 
    ma_parkey, 
    me_parkey , 
    CASE 
        WHEN EXISTS 
            ( 
           SELECT 1 
           FROM   cds_h_gruppe GRP1 
           WHERE  grp1.c_mandant = c_mandant 
           AND    grp1.hist_datum = Add_months(Last_day(Trunc(sysdate)), -1)
           AND    grp1.funktionscode = 'H' 
           AND    grp1.parkey1 = ma_parkey
       ) THEN 1 
        ELSE NULL 
    END ma_me , 
    CASE 
        WHEN EXISTS 
            ( 
           SELECT 1 
           FROM   cds_h_gruppe GRP2 
           WHERE  grp2.c_mandant = c_mandant 
           AND    grp2.hist_datum = Add_months(Last_day(Trunc(sysdate)), -1)
           AND    grp2.funktionscode = 'U' 
           AND    grp2.parkey1 = me_parkey
       ) THEN 1 
        ELSE NULL 
    END me_ma,
    Row_number() OVER (partition BY c_mandant, ma_parkey, me_parkey ORDER BY c_mandant, ma_parkey, me_parkey) anz_ma
FROM     
( 
     SELECT c_mandant, 
     hist_datum, 
     parkey1, 
     parkey2, 
     funktionscode , 
     CASE 
       WHEN funktionscode = 'U' THEN parkey1 
       ELSE parkey2 
     END ma_parkey , 
     CASE 
       WHEN funktionscode = 'U' THEN NULL 
       ELSE parkey1 
     END me_parkey 
     FROM   cds_h_gruppe 
     WHERE  funktionscode IN ('U', 
             'H') 
     AND    hist_datum = Add_months(Last_day(Trunc(sysdate)), -1)

I want to extract table names and column names from the query. The result should look some thing like

Tables:

CDS_H_GRUPPE

Columns:

CDS_H_GRUPPE.c_mandant    
CDS_H_GRUPPE.funktionscode    
CDS_H_GRUPPE.hist_datum    
CDS_H_GRUPPE.parkey1 
CDS_H_GRUPPE.parkey2

Can I extract column names and table names from any complex query string, which is valid for Oracle, SQL server or DB2?

Dale K
  • 21,987
  • 13
  • 41
  • 69
dumbdragon
  • 11
  • 5
  • I would recommend working backwards by Keying off words like `SELECT`, `UPDATE`, and `DELETE`, then delimiting off commas and the `FROM` keyword. Good Luck – Oxymoron Nov 08 '16 at 05:51
  • I don't see any relationship between that SQL and the output you're looking for. Why is `ma_me` not included? – Rob Nov 08 '16 at 06:08

1 Answers1

0

Don't try to parse it yourself, use an SQL Parser. Apparently, Entity Framework used to have one, but no longer does. There seem to be a few more, such as this one from the Irony Project.

There's a commercial parser that you can get for $900 ($1,900 if you want to distribute your code apparently).

Dale K
  • 21,987
  • 13
  • 41
  • 69
zmbq
  • 36,789
  • 13
  • 91
  • 160