Replacing where clause dynamically in query

New_Bee
edited February 11, 2022 in Analytics #1
<p>In my report query I have a where clause that needs to be replaced dynamically based on the data chosen in the front end.<br><br>
The query is something like :<br>
 </p>
<p><em><select statement></em></p>
<p><em>where ?=?</em></p>
<p> </p>
<p> </p>
<p>I already have a code to replace the value - I created report parameter and linked to the value ? in the query. <br><br>
Example: <br><em><select statement><br>
where name=?</em></p>
<p> </p>
<p>Any value of name that comes from front end replaces the ? in the where clause - this works fine.</p>
<p> </p>
<p> </p>
<p>But now I need to replace the entire clause. Should I create two parameters and link them to both the '?' ?<br>
Can anyone help with this?<br><br>
Thanks !</p>

Comments

  • jfranken
    edited February 19, 2016 #2
    <p>One option is to change your query to:</p>
    <p> </p>
    <p>select x from y where myplaceholder1 = 'myplaceholder2'</p>
    <p> </p>
    <p>Then add the parameter values in code.  That is done by selecting the data set and clicking the Script tab.  Go to the beforeOpen event and enter the following:</p>
    <p> </p>
    <p>this.queryText = this.queryText.replace("myplaceholder1", params["p1"].value).replace("myplaceholder2", params["p2"].value);</p>
    <p> </p>
    <p>(replace "p1" and "p2" with the names of your two parameters)</p>
    Warning No formatter is installed for the format ipb
  • <p>Thanks for responding.<br><br>
    But in my report the input parameters are not fixed, as in there could be one or two or even no input parameter. How do I alter the query based on the number of input parameters? </p>
  • <p>Hi New_Bee,</p>
    <p> </p>
    <p>defining a query dynamically is done with coding i.e. you can add or replace part of the query depending on many paramters.</p>
    <p>for instance you can have things like</p>
    <pre class="_prettyXprint _lang-">
    var myWhere = " where (1=1) ";
    if (params["p1"] == 'A' && params["p2"] == 'B') var myWhere = " and field1=" + params["p3"];
    if (params["p1"] == 'A' && params["p2"] != 'B') var myWhere = " and field1=" + params["p4"];
    if (params["p1"] != 'A' && params["p2"] == 'B') var myWhere = " and field1=" + params["p5"];
    if (params["p1"] != 'A' && params["p2"] != 'B') var myWhere = " and field1=" + params["p6"];
    this.queryText += myWhere; </pre>
    <p><a data-ipb='nomediaparse' href='http://developer.actuate.com/community/forum/index.php?/topic/35490-dynamic-query-whole-where-clause-not-standard-field1/'>Take a look on this thread here</a></p>
    Warning No formatter is installed for the format ipb