{"id":333,"date":"2010-07-21T04:24:39","date_gmt":"2010-07-21T09:24:39","guid":{"rendered":"http:\/\/www.dpriver.com\/blog\/?page_id=333"},"modified":"2012-01-29T02:37:46","modified_gmt":"2012-01-29T07:37:46","slug":"remove-sql-variables-in-where-clause-without-broken-condition-expression","status":"publish","type":"page","link":"https:\/\/www.dpriver.com\/blog\/list-of-demos-illustrate-how-to-use-general-sql-parser\/remove-sql-variables-in-where-clause-without-broken-condition-expression\/","title":{"rendered":"Remove SQL\/user defined variables in where clause without broken condition expression"},"content":{"rendered":"

Sometimes you want to modify condition expression in where clause, such as remove a SQL variable in where condition without broken that condition expression or remove all where clause if necessary.<\/p>\n

Take this SQL for example, you want to remove @var1 in where condition, only delete it from where clause doesn’t work, that will make this statement syntax invalid.<\/p>\n

####

    \r\nselect f from tb\r\nwhere f1 between 1 and @var1\r\n<\/pre>\nIllegal statement after remove @var1<\/p>\n\r\nselect f from tb\r\nwhere f1 between 1 and \r\n<\/pre>\nDesired statement after remove @var1, should remove all where clause.<\/p>\n\r\nselect f from tb\r\n<\/pre>\n\nHere is another sample, if you want to remove @var2 in where condition, only delete it from where clause doesn’t work, that will make this statement syntax invalid.\n<\/p>\n\r\nselect f from tb\r\nwhere f1>10 and (@var1+1 between a and @var2)\r\n<\/pre>\nIllegal statement after remove @var1<\/p>\n\r\nselect f from tb\r\nwhere f1>10 and (@var1+1 between a and )\r\n<\/pre>\nDesired statement after remove @var2<\/p>\n\r\nselect f from tb\r\nwhere f1>10\r\n<\/pre>\n\nDownload this demo: C# version<\/a>, Java version<\/a>\n<\/p>\nupdate or remove a user defined variable<\/strong><\/h4>\nTake this sql for example:<\/p>\n\r\nSELECT SUM (d.amt) \r\nFROM   summit.cntrb_detail d \r\nWHERE  d.id = summit.mstr.id \r\n       AND d.system_gift_type IN ( 'OG', 'PLP', 'PGP' ) \r\n       AND d.fund_coll_attrb IN ( '$Institute$' ) \r\n       AND d.fund_acct IN ( '$Fund$' ) \r\n       AND d.cntrb_date >= '$From_Date$' \r\n       AND d.cntrb_date <= '$Thru_Date$' \r\nGROUP  BY d.id \r\n<\/pre>\n\nIn this query, values between two $ e.g '$XXX$' represents some run time fields which user enter. For example '$Fund$' will be replaced with some user entered vale as '0000001'. <\/p>\nNow our requirement is that we need to remove complete join in case user has not entered value specific to  $XXX$ run time field.<\/p>\nConsider example as if user has not entered  '$Fund$' value the we should remove complete join  \"and d.FUND_ACCT in    (   '$Fund$'    )  \" from this query.<\/p>\nGeneral SQL Parser can help you to achieve this quite easily with this demo<\/a>. And even better, this demo can remove \"Where\" clause if and only if single run time condition is presented and user has not provided any input for it.\n<\/p>\n\nDownload this demo: C# version<\/a>, Java version<\/a>\n<\/p>\n\nHere is another example<\/a> that illustrate how to iterate expression in pre-order, in-order and post-order.<\/p>\n","protected":false},"excerpt":{"rendered":"Sometimes you want to modify condition expression in where clause, such as remove a SQL variable in where condition without broken that condition expression or remove all where clause if necessary. Take this SQL for example, you want to remove @var1 in where condition, only delete it from where clause doesn’t work, that will make this statement syntax invalid. select f from tb where f1 between 1 and @var1 Illegal statement after remove @var1 select f from tb where f1 between 1 and Desired statement after remove @var1, should remove all where clause. select f from tb Here is another\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