Search code examples
plsqljasper-reports

How can I make a field optional?


I use Oracle stored procedure in my jrxml report. It returns cursor as a result. I wish to be able to return different column sets, so some columns in cursor may appear or disappear.

However when column which I've described as a field

<field name="MY_COLUMN" class="java.lang.String"/> 

doesn't exist in cursor it throws an error like this:

error while rendering
net.sf.jasperreports.engine.JRException: Unknown column name MY_COLUMN in result set

Is there any way to declare field in JRXML as optional? When the column doesn't exist in the result set.


Solution

  • AFIK: There is no setting/attributes that you can set on your field declaration.

    Your options are:

    1. Always return the column name even if not available using sql alias for example:

      SELECT column1, null as MY_COLUMN from myTable.
      
    2. Create your own JRDatasource which can be all from loading data into beans and passing a JRBeanCollectionDataSource to creating your own JRQueryExecuter and register it to the factory.