{"id":350,"date":"2010-07-21T21:43:38","date_gmt":"2010-07-22T02:43:38","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=350"},"modified":"2011-06-08T02:53:02","modified_gmt":"2011-06-08T07:53:02","slug":"rename-table-names-in-sql-script","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/","title":{"rendered":"Rename table names in SQL script"},"content":{"rendered":"<p>Rename table name in SQL query is one of the most important refactor features you need when you manage your SQL scripts. This demo illustrates how to rename table names in SQL query, you can enhance this demo to meet your own requirement.<\/p>\n<p>\nThis demo renames all tablename to view_tablename, and [tablename] to [view_tablename].\n<\/p>\n<p>Input SQL query:<\/p>\n<pre>\r\nSELECT Table1.f1 AS fld1,\r\n       Table2.F2 AS fld2\r\nFROM   Table1\r\n       LEFT JOIN Table2\r\n         ON Table1.f3 = Table2.f3\r\nWHERE  Table2.f5 > 10\r\nORDER  BY Table2.f6; \r\n<\/pre>\n<p>SQL after change table name:<\/p>\n<pre>\r\nSELECT view_Table1.f1 AS fld1,\r\n       view_Table2.F2 AS fld2\r\nFROM   view_Table1\r\n       LEFT JOIN view_Table2\r\n         ON view_Table1.f3 = view_Table2.f3\r\nWHERE  view_Table2.f5 > 10\r\nORDER  BY view_Table2.f6 \r\n<\/pre>\n<p>\nDownload this demo: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/tablerename\/tableColumnRename.zip\">C# version<\/a>, <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/java\/modifysql\/replaceTablename.java\">Java version<\/a>\n<\/p>\n<p>\nCode in C# to achieve what we mentioned above.\n<\/p>\n<pre>\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Text;\r\nusing System.IO;\r\n\r\nusing gudusoft.gsqlparser;\r\nusing gudusoft.gsqlparser.Units;\r\n\r\nnamespace tablerename\r\n{\r\n    class tablerename\r\n    {\r\n        public static TSourceTokenList maintabletokens;\r\n\r\n        static void Main(string[] args)\r\n        {\r\n\r\n\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n            maintabletokens = new TSourceTokenList(false);\r\n\r\n            sqlparser.OnTableToken += new TOnTableTokenEvent(OnTableTokenHandler);\r\n\r\n            sqlparser.Sqlfilename = args[0];\r\n            int i = sqlparser.Parse();\r\n            if (i == 0)\r\n            {\r\n                foreach (TSourceToken st in maintabletokens)\r\n                { \/\/ modify table name in from clause here\r\n\r\n                    if (st.AsText.StartsWith(\"\\\"\"))\r\n                    {\r\n                        st.AsText = \"\\\"\" + \"view_\" + st.AsText.Substring(1);\r\n                    }\r\n                    else if (st.AsText.StartsWith(\"[\"))\r\n                    {\r\n                        st.AsText = \"[\" + \"view_\" + st.AsText.Substring(1);\r\n                    }\r\n                    else\r\n                    {\r\n                        st.AsText = \"view_\" + st.AsText;\r\n                    }\r\n                }\r\n\r\n                for (int j = 0; j < sqlparser.SqlStatements.Count(); j++)\r\n                {\r\n                    Console.WriteLine(sqlparser.SqlStatements[j].AsText);\r\n                    Console.WriteLine(\"\");\r\n                }\r\n            }\r\n            else\r\n                Console.WriteLine(sqlparser.ErrorMessages);\r\n        }\r\n\r\n\r\n\r\n        static void OnTableTokenHandler(object o, gudusoft.gsqlparser.TSourceToken st, gudusoft.gsqlparser.TCustomSqlStatement stmt)\r\n        {\r\n            \/\/table in this event is constructed as an TLzTable object, usually it's main point where this table occurs in statement\r\n            if ((st.DBObjType == TDBObjType.ttObjTable) )\r\n            {\r\n                if (o is TLzTable)\r\n                { \/\/don't rename table in from clause, otherwise, same table name  in other clause ( such as where clause )may not link to it correctly while parsing.\r\n                    maintabletokens.Add(st);\r\n                }\r\n                else\r\n                {\r\n                    if (st.AsText.StartsWith(\"\\\"\"))\r\n                    {\r\n                        st.AsText = \"\\\"\" + \"view_\" + st.AsText.Substring(1);\r\n                    }\r\n                    else if (st.AsText.StartsWith(\"[\"))\r\n                    {\r\n                        st.AsText = \"[\" + \"view_\" + st.AsText.Substring(1);\r\n                    }\r\n                    else\r\n                    {\r\n                        st.AsText = \"view_\" + st.AsText;\r\n                    }\r\n                }\r\n            }\r\n\r\n        }   \r\n    }\r\n}\r\n<\/pre>\n<p>Download this demo: <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/csharp\/tablerename\/tableColumnRename.zip\">C# version<\/a>, <a href=\"http:\/\/www.dpriver.com\/gsp\/demos\/java\/modifysql\/replaceTablename.java\">Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rename table name in SQL query is one of the most important refactor features you need when you manage your SQL scripts. This demo illustrates how to rename table names in SQL query, you can enhance this demo to meet your own requirement. This demo renames all tablename to view_tablename, and [tablename] to [view_tablename]. Input SQL query: SELECT Table1.f1 AS fld1, Table2.F2 AS fld2 FROM Table1 LEFT JOIN Table2 ON Table1.f3 = Table2.f3 WHERE Table2.f5 > 10 ORDER BY Table2.f6; SQL after change table name: SELECT view_Table1.f1 AS fld1, view_Table2.F2 AS fld2 FROM view_Table1 LEFT JOIN view_Table2 ON view_Table1.f3 =\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":"<!-- This site is optimized with the Yoast SEO plugin v19.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Help you to make better use of General SQL Parser<\/title>\n<meta name=\"description\" content=\"Rename table names in SQL script\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Help you to make better use of General SQL Parser\" \/>\n<meta property=\"og:description\" content=\"Rename table names in SQL script\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and Data Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2011-06-08T07:53:02+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\",\"name\":\"SQL and Data Blog\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"sameAs\":[],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"contentUrl\":\"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png\",\"width\":251,\"height\":72,\"caption\":\"SQL and Data Blog\"},\"image\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\",\"url\":\"https:\/\/www.dpriver.com\/blog\/\",\"name\":\"SQL and Data Blog\",\"description\":\"SQL related blog for database professional\",\"publisher\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/\",\"url\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/\",\"name\":\"Help you to make better use of General SQL Parser\",\"isPartOf\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/#website\"},\"datePublished\":\"2010-07-22T02:43:38+00:00\",\"dateModified\":\"2011-06-08T07:53:02+00:00\",\"description\":\"Rename table names in SQL script\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.dpriver.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"List of demos illustrate how to use general sql parser\",\"item\":\"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Rename table names in SQL script\"}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Help you to make better use of General SQL Parser","description":"Rename table names in SQL script","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/","og_locale":"en_US","og_type":"article","og_title":"Help you to make better use of General SQL Parser","og_description":"Rename table names in SQL script","og_url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/","og_site_name":"SQL and Data Blog","article_modified_time":"2011-06-08T07:53:02+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/www.dpriver.com\/blog\/#organization","name":"SQL and Data Blog","url":"https:\/\/www.dpriver.com\/blog\/","sameAs":[],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","contentUrl":"https:\/\/www.dpriver.com\/blog\/wp-content\/uploads\/2022\/07\/sqlpp-character.png","width":251,"height":72,"caption":"SQL and Data Blog"},"image":{"@id":"https:\/\/www.dpriver.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/www.dpriver.com\/blog\/#website","url":"https:\/\/www.dpriver.com\/blog\/","name":"SQL and Data Blog","description":"SQL related blog for database professional","publisher":{"@id":"https:\/\/www.dpriver.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dpriver.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/","url":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/","name":"Help you to make better use of General SQL Parser","isPartOf":{"@id":"https:\/\/www.dpriver.com\/blog\/#website"},"datePublished":"2010-07-22T02:43:38+00:00","dateModified":"2011-06-08T07:53:02+00:00","description":"Rename table names in SQL script","breadcrumb":{"@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/rename-table-names-in-sql-script\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.dpriver.com\/blog\/"},{"@type":"ListItem","position":2,"name":"List of demos illustrate how to use general sql parser","item":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/"},{"@type":"ListItem","position":3,"name":"Rename table names in SQL script"}]}]}},"_links":{"self":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/350"}],"collection":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/comments?post=350"}],"version-history":[{"count":8,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/350\/revisions"}],"predecessor-version":[{"id":989,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/350\/revisions\/989"}],"up":[{"embeddable":true,"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/pages\/235"}],"wp:attachment":[{"href":"https:\/\/www.dpriver.com\/blog\/wp-json\/wp\/v2\/media?parent=350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}