{"id":257,"date":"2010-07-16T04:27:57","date_gmt":"2010-07-16T09:27:57","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=257"},"modified":"2011-07-13T03:37:30","modified_gmt":"2011-07-13T08:37:30","slug":"visit-sql-statement-recursively","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/visit-sql-statement-recursively\/","title":{"rendered":"Visit SQL statement recursively"},"content":{"rendered":"

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.<\/p>\n

Code in Java:<\/p>\n

    \r\n        TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);\r\n        sqlparser.sqlfilename  = args[0];\r\n\r\n        int ret = sqlparser.parse();\r\n        if (ret == 0){\r\n            for(int i=0;i<sqlparser.sqlstatements.size();i++){\r\n                iterateStmt(sqlparser.sqlstatements.get(i));\r\n            }\r\n        }else{\r\n            System.out.println(sqlparser.getErrormessage());\r\n        }\r\n\r\n    protected static void iterateStmt(TCustomSqlStatement stmt){\r\n        System.out.println(stmt.sqlstatementtype.toString());\r\n        for (int i=0;i<stmt.getStatements().size();i++){\r\n           iterateStmt(stmt.getStatements().get(i));\r\n        }\r\n    }\r\n<\/pre>\n\nDownload this demo: Java version<\/a>, C# version<\/a>\n<\/p>\n\nIf you need to process all elements of a SQL statement, you may use visitor pattern to iterate all parse tree nodes just like this XML demo<\/a> does.<\/p>\n","protected":false},"excerpt":{"rendered":"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. Code in Java: TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle); sqlparser.sqlfilename = args[0]; int ret = sqlparser.parse(); if (ret == 0){ for(int i=0;i<sqlparser.sqlstatements.size();i++){ iterateStmt(sqlparser.sqlstatements.get(i)); } }else{ System.out.println(sqlparser.getErrormessage()); } protected static void iterateStmt(TCustomSqlStatement stmt){ System.out.println(stmt.sqlstatementtype.toString()); for (int i=0;i<stmt.getStatements().size();i++){ iterateStmt(stmt.getStatements().get(i)); } } Download this demo: Java version, C# version If you need to\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"open","ping_status":"open","template":"gsp_feature_page_tt.php","meta":[],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":5}},"yoast_head":"\nHelp you to make better use of General SQL Parser<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n