Differences between queries and imported SQL files
For users who wish to write their own SQL statement, JReport enables them to put the SQL statement into a plain text file (.txt or .sql) and then import the file into a catalog and JReport can load data from this file. At present each SQL file can only contain one SQL statement.
The imported SQL files can work the same as queries in JReport, for example, you can use imported SQL files to create page reports directly, use the Data Manager to control data retrieval of the imported SQL files, and create cached result files for the imported SQL files.
However there are still some differences between queries and imported SQLs:
Imported SQL files can also be use to build queries and business views.
The SQL statement here supports the SQL 92 standard, although for different databases, it may vary. The basic statement of an SQL file is:
SELECT...FROM...WHERE
Nested queries are also supported.
|
When you write the SQL statement, you can use parameters and constant level formulas predefined in the catalog data source in which the JDBC connection is created in the format @FieldName or :FieldName to calculate your data. For example, if you need to get different result sets from the SQL file at runtime, you can reference parameters in the WHERE clause of the SQL statement by to dynamically filter the data. In the above SQL example, if you want to use a parameter to return a result set in which the customer ID is greater than the parameter, then the WHERE clause would be like this:
WHERE (Products."Product Type ID"=Catalog."Product Type ID") AND ("Orders Detail"."Product ID"=Products."Product ID") AND (Orders."Customer ID"=Customers."Customer ID") AND (Orders."Order ID"="Orders Detail"."Order ID") AND ( ( Customers.Country='USA' )) AND (Customers."Customer ID" > @pID)
Where, pID is a parameter created in the catalog.
However, if a parameter allows multiple values and is enabled to use one single value "All" to represent all its values (the parameter's Enable the "All" Option is true), to use the parameter in the SQL statement, you should embed the IN condition in parenthesis to enable the "All" value to work properly, for example:
select... from ... where ... and ... and (country in @pCountry) and (customerid in @pID)
In addition, in an Oracle database, if you want to use a Date or DateTime type parameter or a specific date or time in the WHERE clause of the SQL statement to filter the data, you need to add the to_date() or to_timestamp() function in the clause, for example:
WHERE (@COL_DATE = to_date(@p_DATE,'yyyy-mm-dd')
WHERE (@COL_DATE = to_date('2003-12-06','yyyy-mm-dd')
Besides using parameters, you can also use the special field User Name as @username in the WHERE clause of the SQL statement to filter the data.
After you have set up the JDBC connection and created the SQL files, you can import them into a catalog.
The SQL file is then added in the Imported SQLs node in the catalog resource tree. You can right-click it and select Show SQL from the shortcut menu to view the statement of SQL file if you want. The format of the SQL file, such as comments, are maintained when it is added into the JReport catalog.
If you make any changes to an SQL file, you need to update it in the JReport catalog so that reports built on it can work properly.