2013-06-20 104 views
1

我使用General SQL Parser解析SQL Select查詢,目前還不清楚的是GSP文檔中並沒有說如何提取subquerys,舉例如果我有以下SQL:通用SQL解析器,獲取SQL查詢子查詢

select * from table1 join (subquery) as T2 on table1.a=T2.a 

其中子查詢是另一個選擇查詢重新。 ?如何獲得子查詢字符串,這樣我就可以在其應用GSP規則

的幫助

回答

2

這裏非常感謝是演示代碼,他們提供...(taken from here)

「 這個演示說明如何快速查找腳本中的各種SQL語句查找PLSQL塊/ package/procedure/function中的所有SQL語句;在select/delete/update語句中查找嵌套的子查詢;查找union select中的查詢聲明;查找where子句中的子查詢,選擇列表等。 「

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 

using gudusoft.gsqlparser; 
using gudusoft.gsqlparser.Units; 

namespace findsubquerys 
{ 
    class prg 
    { 
     static void Main(string[] args) 
     { 
      int c = Environment.TickCount; 

      if (args.Length == 0) 
      { 
       Console.WriteLine("{0} scriptfile", "syntaxcheck"); 
       return; 
      } 

      TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql); 
      sqlparser.Sqlfilename = args[0]; 
      int iRet = sqlparser.Parse(); 

      if (iRet == 0) 
      { 
       foreach (TCustomSqlStatement stmt in sqlparser.SqlStatements) 
       { 
        printStmt(stmt); 
       } 

      } 
      else 
      { 
       Console.WriteLine("Syntax error found in input sql:"); 
       Console.WriteLine(sqlparser.ErrorMessages); 

      } 
     } 

     static void printStmt(TCustomSqlStatement pstmt) 
     { 
      Console.WriteLine(pstmt.AsText+"\n"); 

      for (int j = 0; j < pstmt.ChildNodes.Count(); j++) 
      { 
       if (pstmt.ChildNodes[j] is TCustomSqlStatement) 
       { 
        printStmt((TCustomSqlStatement)(pstmt.ChildNodes[j])); 
       } 
      } 

     } 

    } //class 
} 
+0

Thx例如,我設法運行您的代碼,printStmt以新行打印每條語句。這裏是輸出: –