Question

Is it possible to make the schema name dynamic in a BIRT query.

I tried this:

SELECT CURRENT DATE AS DATE, 
(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'
      END) AS DAYOFWEEK
FROM **?**.COBOL_CALENDAR
 WHERE SERVICE_DATE = CURRENT DATE"

This generates the following error: The following items have errors:

ReportDesign (id = 1): 
+ Cannot get the result set metadata.
SQL statement does not return a ResultSet object.
SQL error #1: [IBM][CLI Driver][DB2] SQL0104N  An unexpected token "?" was found following "".  Expected tokens may include:  "( TABLE FINAL <IDENTIFIER> XMLTABLE".  SQLSTATE=42601

But the ? only seems to work for the where clause.

I need to pass the schema as a param and use dynamically because it changes based on dev/cat/prod

is there not some way to genereate sql outsite of the birt xml and inject it somehow??

I did some more searching on the subject and found this solution

<method name="beforeOpen"><![CDATA[this.queryText = "SELECT CURRENT DATE AS DATE, "+
"(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'"+
"      END) AS DAYOFWEEK"+
"FROM "+params["SCHEMA"]+".COBOL_CALENDAR"+
" WHERE SERVICE_DATE = CURRENT DATE";]]></method>

However regardless of how many examples that are out there on this issue injecting sql in this manner only generates the following error.

ReportDesign (id = 1): 
+ Cannot get the result set metadata.
SQL statement does not return a ResultSet object.
SQL error #1: [IBM][CLI Driver][DB2] SQL0104N  An unexpected token "SCHEMANAME" was found following "".  Expected tokens may include:  ", FROM INTO".  SQLSTATE=42601

I even tried the reportContext.getParameterValue("SCHEMANAME") route with the same results.

Was it helpful?

Solution

ARRRGH it was a stupid problem with spaces!!

" END) AS DAYOFWEEK"+ "FROM "+params["SCHEMA"]+".COBOL_CALENDAR"+

Adding a space in front of the FROM fixed it.

bangs head on lcd display.

OTHER TIPS

Looks like your edited the XML-Source of your report. Here is a more graphical way of doing the replacement of an arbitrary string in your SQL-Query:

Write your query as the following:

SELECT CURRENT DATE AS DATE, 
(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'
      END) AS DAYOFWEEK
FROM dev.COBOL_CALENDAR
 WHERE SERVICE_DATE = CURRENT DATE

If dev is a valid schema you are now able to select the meta-data-set and are still able to replace it with a report parameter.

Next click on your data-set and select the "script" Tab. Here you select "beforeOpen" and enter the replacement ccript:

this.queryText = this.queryText.replace("dev", params["SCHEMA"].value);

This way you replace the String dev in your query text with the value of your SCHEMA Parameter before your execute the query. You could replace every String you want (linke **?** from your question, but with a valid schema in the first place you can use the meta-data-set for the design phase. enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top