{"id":320,"date":"2010-07-21T03:36:31","date_gmt":"2010-07-21T08:36:31","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=320"},"modified":"2010-12-10T02:03:21","modified_gmt":"2010-12-10T07:03:21","slug":"analyze-2-select-statements-then-merge-into-one-statement","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/analyze-2-select-statements-then-merge-into-one-statement\/","title":{"rendered":"Analyze 2 select statements, then merge into one statement"},"content":{"rendered":"

This demo illustrates how to get columns from select list and tables in from clause of 2 select statements, and then create a new select statement based on these columns and tables information.<\/p>\n

Input select statement 1:<\/p>\n

    \r\nSELECT product_id, product_name, product_type\r\nFROM products\r\nWHERE product_type = \"widget\"\r\n<\/pre>\nInput select statement 2:<\/p>\n\r\nSELECT product_id, product_name, product_ price\r\nFROM products\r\nWHERE product_price < 100\r\n<\/pre>\nGenerated select statement :<\/p>\n\r\nSELECT product_id, product_name, product_type, product_ price\r\nFROM products\r\nWHERE product_type = \u201cwidget\u201d AND product_price < 100    \r\n<\/pre>\nCode to process select statement in C#<\/p>\n\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing gudusoft.gsqlparser;\r\nusing gudusoft.gsqlparser.Units;\r\n\r\nnamespace buildsql\r\n{\r\n\r\n    class add2sqls\r\n    {\r\n        string sqlstr1 = @\"SELECT product_id, product_name, product_type FROM products WHERE product_type = \u201cwidget\u201d\"\r\n            , sqlstr2 = @\"SELECT product_id, product_name, product_ price FROM products WHERE product_price < 100\";\r\n        \r\n        TSelectSqlStatement sql1, sql2;\r\n\r\n        public string addsql()\r\n        {\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n            TGSqlParser sqlparser2 = new TGSqlParser(TDbVendor.DbVMssql);\r\n\r\n            sqlparser.SqlText.Text = sqlstr1;\r\n            int ret = sqlparser.Parse();\r\n            if (ret != 0)  return sqlparser.ErrorMessages;\r\n            sql1 = sqlparser.SqlStatements[0] as TSelectSqlStatement;\r\n\r\n            \r\n\r\n            sqlparser2.SqlText.Text = sqlstr2;\r\n            ret = sqlparser2.Parse();\r\n            if (ret != 0) return sqlparser2.ErrorMessages;\r\n            sql2 = sqlparser2.SqlStatements[0] as TSelectSqlStatement;\r\n\r\n            \/\/get field name\r\n            StringBuilder flds = new StringBuilder();\r\n            int p = 0;\r\n            foreach (TLzField fd in sql1.Fields)\r\n            {\r\n\r\n                if (++p> 1) flds.Append(\",\");\r\n                flds.Append(fd.AsText);\r\n            }\r\n\r\n            foreach (TLzField fd in sql2.Fields)\r\n            {\r\n                if(flds.ToString().IndexOf(fd.AsText) == -1)\r\n                flds.Append(\",\"+fd.AsText);\r\n            }\r\n\r\n            \r\n\r\n            \/\/get tablename name\r\n            StringBuilder tlbs = new StringBuilder();\r\n            p = 0;\r\n            foreach (TLzTable tl in sql1.Tables)\r\n            {\r\n\r\n                if (++p > 1) tlbs.Append(\",\");\r\n                tlbs.Append(tl.AsText);\r\n            }\r\n\r\n            foreach (TLzTable tl in sql2.Tables)\r\n            {\r\n                if (tlbs.ToString().IndexOf(tl.AsText) == -1)\r\n                    tlbs.Append(\",\" + tl.AsText);\r\n            }\r\n\r\n            \/\/ Console.WriteLine(\"sb: \"+sb.ToString());\r\n            \/\/ get fields list from sql1\r\n\r\n            String wherestr = \"\";\r\n            if (sql1.WhereClause != null)\r\n            {\r\n                wherestr = sql1.WhereClauseText;\r\n            }\r\n\r\n            if (sql2.WhereClause != null)\r\n            {\r\n                wherestr = wherestr+\" and \"+sql2.WhereClauseText;\r\n            }\r\n\r\n            return \"select \" + flds.ToString() + \" from \" + tlbs.ToString() + \" where \"+wherestr;\r\n        }\r\n    }\r\n}\r\n<\/pre>\nDownload this demo: C# version<\/a>, Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"This demo illustrates how to get columns from select list and tables in from clause of 2 select statements, and then create a new select statement based on these columns and tables information. Input select statement 1: SELECT product_id, product_name, product_type FROM products WHERE product_type = “widget” Input select statement 2: SELECT product_id, product_name, product_ price FROM products WHERE product_price < 100 Generated select statement : SELECT product_id, product_name, product_type, product_ price FROM products WHERE product_type = \u201cwidget\u201d AND product_price < 100 Code to process select statement in C# using System; using System.Collections.Generic; using System.Text; using gudusoft.gsqlparser; using gudusoft.gsqlparser.Units; namespace\u2026\n<\/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