【问题标题】:General SQL Parser , get subquery out of SQL通用 SQL 解析器,从 SQL 中获取子查询
【发布时间】:2013-06-20 14:19:56
【问题描述】:

我正在使用General SQL Parser解析SQL Select查询,不清楚的是GSP的文档没有说明如何提取子查询,例如如果我有以下SQL:

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

其中子查询又是另一个选择查询。如何获取子查询字符串,以便对其应用 GSP 规则?

非常感谢您的帮助

【问题讨论】:

    标签: c# sql parsing select


    【解决方案1】:

    这是他们提供的演示代码... (taken from here):

    " 本演示说明如何快速找出脚本中的各种 SQL 语句。找出 PLSQL 块/包/过程/函数中的所有 SQL 语句;在 select/delete/update 语句中找出嵌套子查询;找出联合选择语句中的查询;找出 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
    }
    

    【讨论】:

    • Thx 例如,我设法运行您的代码, printStmt 在新行中打印每个语句。这是输出:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多