{"id":357,"date":"2010-09-16T20:20:43","date_gmt":"2010-09-17T01:20:43","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=357"},"modified":"2021-03-09T10:00:38","modified_gmt":"2021-03-09T15:00:38","slug":"powerbuilder-datawindow-sql-changes-made-easy","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/powerbuilder\/powerbuilder-datawindow-sql-changes-made-easy\/","title":{"rendered":"PowerBuilder &#8211; Datawindow SQL changes made easy"},"content":{"rendered":"<p>One application I developed involved retrieving from a large dataset based on any number of criteria. Yea, I know, this sounds just like the application you worked on right? Anyway, there are a variety of ways to achieve this in PB but the one approach I like best when dealing with a variable number of search parameters is to modify the datawindow object SQL prior to the retrieve of the data.<\/p>\n<p>Except in my very first PB job, I have not worked anywhere with the requirement of &#8216;database independence&#8217;. All the apps I&#8217;ve coded run against a specific database (Oracle, SQL Server, SQL Anywhere, etc.). As such I almost never create datawindow objects in graphic mode &#8211; I simply find this too restrictive. To implement the type of functionality this article is about, you need to have your Datasource in &#8216;Convert to Syntax&#8217; mode.<\/p>\n<p>Now you have something like this:<\/p>\n<pre name=\"code\" class=\"SQL\">  SELECT pm.partNo,   \n         pm.partDesc,   \n         cm.catId,   \n         cm.catDesc,   \n         gp.productGrpDesc,   \n         pa.availableAmt,\n\t um.uomCode,\n\t lm.locationCode\t\t  \n    FROM dbo.PartMaster pm\n\tJOIN dbo.catMaster cm on pm.catKey = cm.catKey\n\tJOIN dbo.productGrpMaster gp on pm.productGrpKey = gp.procustGrpKey\n\tLEFT JOIN dbo.locationMaster lm on pm.partKey = lm.partKey\n\tLEFT JOIN dbo.partAvailability pa on lm.locationKey = pa.locationKey\n\tJOIN dbo.uomMaster um on pa.uomKey = um.uomKey\n   WHERE 1 = 1<\/pre>\n<p>Notice that there are no retrieval arguments in the statement.<\/p>\n<p>Add &#8216;placeholders&#8217; to the SQL in the form of comments. In the case of SQL Server you can use a double dash (&#8211;). Decide on a standard for these for ease of use. Our sql now looks like:<\/p>\n<pre name=\"code\" class=\"SQL\">  SELECT pm.partNo,   \n         pm.partDesc,   \n         cm.catId,   \n         cm.catDesc,   \n         gp.productGrpDesc,   \n         pa.availableAmt,\n\t um.uomCode,\n\t lm.locationCode\t\t  \n    FROM dbo.PartMaster pm\n\tJOIN dbo.catMaster cm on pm.catKey = cm.catKey --cat_j\n\tJOIN dbo.productGrpMaster gp on pm.productGrpKey = gp.procustGrpKey --pg_j\n\tLEFT JOIN dbo.locationMaster lm on pm.partKey = lm.partKey --lm_j\n\tLEFT JOIN dbo.partAvailability pa on lm.locationKey = pa.locationKey --pa_j\n\tJOIN dbo.uomMaster um on pa.uomKey = um.uomKey\n   WHERE 1 = 1 --cat --pg --lm --pa<\/pre>\n<p>The placeholders on the JOIN lines can be used to limit items returned to a single specified value. To use lists of values you use the placeholders in the WHERE clause.<\/p>\n<p>Many times you will see manipulation of the datawindow SQL being done in the sqlpreview event. For the purposes of this, however, you need to change the sql prior to the actual retrieve (in a &#8216;pre&#8217; retreive event) since the comments (our placeholders) are not passed to the database (they are stripped off and don&#8217;t appear in the sqlpreview event). Use either of the following to obtain the SQL from the datawindow object:<\/p>\n<pre name=\"code\" class=\"VB\">string ls_sql\nls_sql = dw_1.Object.DataWindow.Table.Select\nls_sql = dw_1.getsqlselect( )<\/pre>\n<p>Now you have to generate the strings you want to insert into the SQL using whatever technique you have developed. Then replace the placeholder(s) with the desired string(s).<\/p>\n<pre name=\"code\" class=\"VB\">string ls_cat_j, ls_new_sql\nls_cat_j = &#039;AND cm.catKey = 12345 &#039;\nls_new_sql = replace(ls_sql, Pos(ls_sql,&#039;--cat_j&#039;), 7, ls_cat_j) \n\/\/ &#039;7&#039; is the length of &#039;--cat_j&#039;<\/pre>\n<p>And finally put the new SQL into play with:<\/p>\n<pre name=\"code\" class=\"VB\">dw_1.Object.DataWindow.Table.Select = ls_new_sql<\/pre>\n<p>If you are dealing with an updateable datawindow you should use the SetSQLSelect method (read the Help for more details).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One application I developed involved retrieving from a large dataset based on any number of criteria. Yea, I know, this sounds just like the application you worked on right? Anyway, there are a variety of ways to achieve this in PB but the one approach I like best when dealing with a variable number of&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,10],"tags":[21,12,16],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/357"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=357"}],"version-history":[{"count":8,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":1819,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/357\/revisions\/1819"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}