Home DevShare Designing BIRT Reports

Ways to specify Dynamic Query

Share

by Megha Nidhi Dahal

DevShare - 10 postsForum - 100 postsCommunity AdvisorPoll VoterContributor Contest - 1 win
Posted 02 Nov 2010 - 10:46 PM

(4)  (0)   (1973 views)

Different ways to change the query string in run time. Changing the clauses based upon the parameters passed.

Birt Version:-2.6

Hi All,

I googled about dynamic query in BIRT a few days back, but I got little idea on how to do so though this article was informative click here.

So I thought of sharing few things that I learnt by just playing around. I encountered two ways of doing it, though there may be many, these two are quite easy to pick and are very useful.

1)Using Parameters:

While creating the data set, write the query like a normal sql query with unknown parameters replaced by the symbol ?.

Eq: select * from CLASSICMODELS.EMPLOYEES where OFFICECODE = ?

Click Finish, then goto the parameters tab, BIRT would have identified the ? symbol by now and would have already listed it there as a query parameter. Give a suitable name and link it to report parameter, if no suitable parameters to link, create a new report parameter and link.

The value passe to the report parameter will get replaced in the query above.

2)Using Scripts:

While creating the dataset, write the query like a normal sql query but replace the dynamic text with some special text.

Eg: select * from CLASSICMODELS.EMPLOYEES where OFFICECODE = ':officeCode'

This is useful if same parameter is to be used at multiple places.

Now, in the data set's beforeOpen method (click the desired dataset and click on the script tab in designer to reach to this method), the query string can be accessed by the variable queryText.


Since queryText is a normal string, it can be modified easily as a normal string:
this.queryText = this.queryText.replaceAll(":officeCode", params["officeCode"]);
Where params["officeCode"] is a report parameter.

regards
Arpan


Forum - 1 post

Dipin

Posted: 29 Mar 2011 - 01:03 PM

Arpan,I found this really helpful. Do you know if this works for Multi-select filters as well ? As for instance, param["officeCode] is a muti select filter, where do I define 'IN' operator? I would really appreciate your help.

DevShare - 10 postsForum - 100 postsCommunity AdvisorPoll Voter

Megha Nidhi Dahal

Posted: 29 Mar 2011 - 09:18 PM

select * from CLASSICMODELS.EMPLOYEES where OFFICECODE IN (':officeCodes')Pass the multiple comma separated codes in this parameter and the same approach above should work.

Forum - 10 posts

Spuri

Posted: 28 Mar 2012 - 05:29 AM

If i need to remove the parameter condition....E.g.1) select * from CLASSICMODELS.EMPLOYEES where OFFICECODE = ':something';2) select * from CLASSICMODELS.EMPLOYEES;Do you know the method to do the same dynamically??? Please replyThanks
 
Filter More