{"id":723,"date":"2011-01-18T03:32:40","date_gmt":"2011-01-18T08:32:40","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=723"},"modified":"2011-01-18T03:46:43","modified_gmt":"2011-01-18T08:46:43","slug":"get-columns-in-select-list","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/get-columns-in-select-list\/","title":{"rendered":"Get columns in select list"},"content":{"rendered":"

It’s quite simple and straightforward to get column information in select list by using a SQL Parser which maybe hard to achieve when using other tools like regex when query become more complex such as including nested subquery.<\/p>\n

Below is a simple Java program that get all columns in select list:<\/p>\n

    \r\npackage demos.gettablecolumns;\r\n\r\nimport gudusoft.gsqlparser.TCustomSqlStatement;\r\nimport gudusoft.gsqlparser.EDbVendor;\r\nimport gudusoft.gsqlparser.ESqlStatementType;\r\nimport gudusoft.gsqlparser.TGSqlParser;\r\nimport gudusoft.gsqlparser.nodes.TResultColumnList;\r\nimport gudusoft.gsqlparser.stmt.TSelectSqlStatement;\r\n\r\nimport java.io.File;\r\n\r\npublic class getResultColumn  {\r\n\r\n    public static void main(String args[])\r\n     {\r\n         TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle);\r\n         sqlparser.sqltext  = \"select a,b,c,DATETOSTRING(date_attr_name,'mm\/dd\/yyyy') from tbl_a\";\r\n         int ret = sqlparser.parse();\r\n         if (ret == 0){\r\n             TSelectSqlStatement select = (TSelectSqlStatement)sqlparser.sqlstatements.get(0);\r\n             TResultColumnList columns = select.getResultColumnList();\r\n             for(int i = 0; i < columns.size();i++){\r\n                 System.out.println(columns.getResultColumn(i).toString());\r\n             }\r\n\r\n         }else{\r\n             System.out.println(sqlparser.getErrormessage());\r\n         }\r\n         \r\n     }\r\n\r\n}\r\n<\/pre>\nThe output of is:<\/p>\n\r\na\r\nb\r\nc\r\nDATETOSTRING(date_attr_name,'mm\/dd\/yyyy')\r\n<\/pre>\nDownload this demo: Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"It’s quite simple and straightforward to get column information in select list by using a SQL Parser which maybe hard to achieve when using other tools like regex when query become more complex such as including nested subquery. Below is a simple Java program that get all columns in select list: package demos.gettablecolumns; import gudusoft.gsqlparser.TCustomSqlStatement; import gudusoft.gsqlparser.EDbVendor; import gudusoft.gsqlparser.ESqlStatementType; import gudusoft.gsqlparser.TGSqlParser; import gudusoft.gsqlparser.nodes.TResultColumnList; import gudusoft.gsqlparser.stmt.TSelectSqlStatement; import java.io.File; public class getResultColumn { public static void main(String args[]) { TGSqlParser sqlparser = new TGSqlParser(EDbVendor.dbvoracle); sqlparser.sqltext = “select a,b,c,DATETOSTRING(date_attr_name,’mm\/dd\/yyyy’) from tbl_a”; int ret = sqlparser.parse(); if (ret == 0){ TSelectSqlStatement select = (TSelectSqlStatement)sqlparser.sqlstatements.get(0);\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":235,"menu_order":0,"comment_status":"closed","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