Ben.Yan
Ben.Yan

Reputation: 118

General SQL Parser , get subquery out of SQL

I am using General SQL Parser to parse a SQL Select query, what is unclear is the document of GSP does not say how to extract subquerys, For Example if i have the following SQL:

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

where subquery is another select query all over again. how do i get subquery string so i can apply GSP rule over it ?

Many thanks for the help

Upvotes: 1

Views: 1852

Answers (1)

BLoB
BLoB

Reputation: 9725

Here is demo code that they provide... (taken from here):

" This demo illustrate how to find out all kinds of SQL statements in a script quickly. Find out all SQL statements inside a PLSQL block/package/procedure/function; Find out nested subquery in a select/delete/update statement; Find out querys in a union select statement; Find out sub-queries in where clause, select list and etc. "

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
}

Upvotes: 2

Related Questions