从任何" SQL查询中提取列名和表名"串

时间:2016-11-08 05:49:58

标签: c# sql

考虑我有一个像这样的复杂的SQL查询..

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)

我想从查询中提取表名和列名。结果应该看起来像

表:

CDS_H_GRUPPE

列:

CDS_H_GRUPPE.c_mandant

CDS_H_GRUPPE.funktionscode

CDS_H_GRUPPE.hist_datum

CDS_H_GRUPPE.parkey1 

CDS_H_GRUPPE.parkey2

我可以从任何对oracle,SQL server或DB2有效的复杂查询字符串中提取列名和表名吗?

1 个答案:

答案 0 :(得分:0)

请勿尝试自己解析它,使用SQL Parser。显然,实体框架used to have one,但不再如此。似乎还有一些,例如来自Irony项目的this one

你可以以900美元的价格获得commercial parser(如果你想明显分发旅游代码,可以获得1,900美元)。