{"id":343,"date":"2010-07-21T04:47:59","date_gmt":"2010-07-21T09:47:59","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=343"},"modified":"2013-04-17T21:24:21","modified_gmt":"2013-04-18T02:24:21","slug":"replace-constant-in-sql-with-sql-variables","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/replace-constant-in-sql-with-sql-variables\/","title":{"rendered":"Replace constant in SQL with SQL variables"},"content":{"rendered":"

Hard code string constant in SQL query is not a good practice, it should be replaced by SQL variable. General SQL Parser can help you to achieve this quickly.<\/p>\n

Here is an insert statement with string constant, those string constants can be replaced by SQL variable in your program automatically.<\/p>\n

    \r\nINSERT INTO employee(employeename,lastname) VALUES ('arun','deep')\r\n<\/pre>\nSQL after replace string constant<\/p>\n\r\nINSERT INTO employee(employeename,lastname) VALUES (@var1,@var2)\r\n<\/pre>\nCode in C# to achieve what we mentioned above.<\/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 replaceconstwithvar\r\n{\r\n    class replaceconstwithvar\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            \/\/ create a sql parser instance supports ms sql server sql dialect\r\n            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);\r\n\r\n            sqlparser.SqlText.Text = @\"INSERT INTO employee(employeename,lastname) VALUES ('arun','deep')\";\r\n            int iRet = sqlparser.Parse();\r\n            int counter = 0;\r\n            if (iRet == 0)\r\n            {\r\n                foreach (TCustomSqlStatement sql in sqlparser.SqlStatements)\r\n                {\r\n                    foreach (TSourceToken st in sql.SourceTokenList)\r\n                    {\r\n                        if \r\n                            (\r\n                            (st.TokenType == TTokenType.ttNumber)\r\n                            || (st.TokenType == TTokenType.ttSQString)\r\n                            )\r\n                        {\r\n                            counter++;\r\n                            st.AsText = \"@var\" + counter.ToString();\r\n\r\n                        }\r\n                    }\r\n\r\n                    Console.WriteLine(\"{0}\", sql.AsText);\r\n                }\r\n            }\r\n            else\r\n            {\r\n                Console.WriteLine(\"Syntax error found in input sql:\");\r\n                Console.WriteLine(sqlparser.ErrorMessages);\r\n            }\r\n\r\n        }\r\n    }\r\n}\r\n<\/pre>\nDownload this demo: C# version<\/a>, Java version<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"Hard code string constant in SQL query is not a good practice, it should be replaced by SQL variable. General SQL Parser can help you to achieve this quickly. Here is an insert statement with string constant, those string constants can be replaced by SQL variable in your program automatically. INSERT INTO employee(employeename,lastname) VALUES (‘arun’,’deep’) SQL after replace string constant INSERT INTO employee(employeename,lastname) VALUES (@var1,@var2) Code in C# to achieve what we mentioned above. using System; using System.Collections.Generic; using System.Text; using gudusoft.gsqlparser; using gudusoft.gsqlparser.Units; namespace replaceconstwithvar { class replaceconstwithvar { static void Main(string[] args) { \/\/ create a sql parser\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