多次重用参数

Reusing parameters more than once

我正在用 BIRT 写报告。我的数据集有两个输入参数。

我想在我的 where 子句中使用两次的第二个参数。我发现我可以使用 with 子句来执行此操作。请注意,这是一个 postgresql 数据库,所以我不需要 from dual.

我的sql如下:

with "params" as (select ? as "sname", ? as "ename")
select "user"."fullName", "user"."address1", "user"."address2", "user"."city", "provinces"."abbreviation", "user"."postalcode", "client"."companyName"
from "user", "params"
LEFT JOIN "client" on "user"."client" = "client"."id" 
LEFT JOIN "provinces" on "user"."province" = "provinces"."id"
WHERE "user"."fullName" >= "params"."sname" and (("user"."fullName" <= "params"."ename") or ("params"."ename" =''))`

当我尝试运行此操作或在 BIRT 的编辑数据集屏幕中预览结果时,出现以下错误:

Error happened while running the report. at .... Caused by: org.eclipse.birt.report.data.adapter.api.AdapterException: An exception occurred during processing. Please see the following message for details: Failed to prepare the query execution for the data set: user Cannot get the result set metadata. org.eclipse.birt.report.data.oda.jdbc.JDBCException: SQL statement does not return a ResultSet object. SQL error #1:ERROR: invalid reference to FROM-clause entry for table "user" Hint: There is an entry for table "user", but it cannot be referenced from this part of the query. Position: 252;

我受不了了。从我所看到的一切来看,它应该可以工作。

您可以在 Birt 中的 SQL-Query 中添加多个参数,并根据需要重复使用 Birt 中的输入参数。 .

因此您可以在不使用 with-clause 的情况下编写查询并防止显式和隐式连接的混合:

select "user"."fullName", "user"."address1", "user"."address2", "user"."city", "provinces"."abbreviation", "user"."postalcode", "client"."companyName"
from "user"
LEFT JOIN "client" on "user"."client" = "client"."id" 
LEFT JOIN "provinces" on "user"."province" = "provinces"."id"
WHERE "user"."fullName" >= ? and (("user"."fullName" <= ?) or (? =''))