How to create a BIRT dataset that accepts multiple (CSV) values that it can be used inside “IN” clause in select statement

余生颓废 提交于 2019-11-29 16:45:12

We cannot do this with a regular SQL parameter '?'.

A workaround is to replace this '?' by a default value in the query, and dynamically inject an appropriate comma-separated list of values in the "beforeOpen" script of the dataset:

Default query

Assuming the datatype of ID is an integer, set up the query like this (of course use here a valid ID to be able to preview data):

select * from table where ID in ( 1000 )

"beforeOpen" script of the dataset:

   this.queryText=this.queryText.replaceAll('1000',params["parameter_name"].value.join(","));

This way, if "parameter_name" returns 3 values 1100,1200,1300 the query sent to the database will be:

select * from table where ID in ( 1100,1200,1300)

It is similar if the datatype of ID is a String, we just have to play a little bit with quotes. However with a String type this kind of handling makes SQL Injection attacks possible, we should firstly check if parameter values look like what we expect.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!