Question

I have a birt dataset for a db2 query. My query works fine without parameters with the following query...

with params as (SELECT '2014-02-16' enddate,'1' locationid FROM sysibm.sysdummy1) 
select 
   t.registerid
from (
select 
   ...
FROM params, mytable sos
WHERE sos.locationid=params.locationid
AND sos.repositorytype ='xxx' 
AND sos.repositoryaccountability='xxx' 
AND sos.terminalid='xxx'
AND DATE(sos.balanceDate) between date(params.enddate)-6 DAY and date(params.enddate)
GROUP BY sos.terminalid,sos.balancedate,params.enddate) t
GROUP BY 
   t.registerid
WITH UR

But when I change the top line to ...

 with params as (SELECT ? enddate,? locationid FROM sysibm.sysdummy1) 

And make the two input paramters of string datatype I get db2 errors sqlcode -418. But i know that it is not my querty because my query works.

What is the right way for me to set up the parameters so there is no error?

thanks

Was it helpful?

Solution

I'm not familiar with DB2 programming, but on Oracle the ? works anywhere in the query.

Have you looked at http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2z9.doc.codes%2Fsrc%2Ftpc%2Fn418.htm?

Seems that on DB2 it's a bit more complicated and you should use "typed parameter markers".

The doc says:

Typed parameter marker

A parameter marker that is specified with its target data type. A typed parameter marker has the general form: CAST(? AS data-type) This invocation of a CAST specification is a "promise" that the data type of the parameter at run time will be of the data type that is specified or some data type that is assignable to the specified data type.

Apart from that, always assure that your date strings are in the format that the DB expects, and use explicit format masks in the date function, like this:

with params as (
    SELECT cast (? as varchar(10)) enddate,
           cast (? as varchar2(80)) locationid
    FROM sysibm.sysdummy1
)
select
...
from params, ...
where ...
AND DATE(sos.balanceDate) between date(XXX(params.enddate))-6 DAY and date(XXX(params.enddate))
...

Unfortunately I cannot tell you how the XXX function should look on DB2. On Oracle, an example would be

to_date('2014-02-18', 'YYYY-MM-DD')

On DB2, see Converting a string to a date in DB2

OTHER TIPS

In addition to hvb answer, i see two options:

Option 1 you could use a DB2 stored procedure instead of a plain SQL query. Thus there won't be these limitations you face to, due to JDBC query parameters.

Option 2, we should be able to remove the first line of the query "with params as" and replace it with question marks within the query:

select 
   t.registerid
from (
select 
   sos.terminalid,sos.balancedate,max(sos.balanceDate) as maxdate
FROM params, mytable sos
WHERE sos.locationid=?
AND sos.repositorytype ='xxx' 
AND sos.repositoryaccountability='xxx' 
AND sos.terminalid='xxx'
AND DATE(sos.balanceDate) between date(?)-6 DAY and date(?)
GROUP BY sos.terminalid,sos.balancedate) t
GROUP BY 
   t.registerid

A minor drawback is, this time we need to declare 3 dataset parameters in BIRT instead of 2. More nasty, i removed params.endDate from "group by" and replaced it with "max(sos.balanceDate)" in select clause. This is very near but not strictly equivalent. If this is not acceptable in your context, a stored procedure might be the best option.

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