1

是否可以在 BIRT 查询中使模式名称动态化。

我试过这个:

SELECT CURRENT DATE AS DATE, 
(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'
      END) AS DAYOFWEEK
FROM **?**.COBOL_CALENDAR
 WHERE SERVICE_DATE = CURRENT DATE"

这会产生以下错误:以下项目有错误:

ReportDesign (id = 1): 
+ Cannot get the result set metadata.
SQL statement does not return a ResultSet object.
SQL error #1: [IBM][CLI Driver][DB2] SQL0104N  An unexpected token "?" was found following "".  Expected tokens may include:  "( TABLE FINAL <IDENTIFIER> XMLTABLE".  SQLSTATE=42601

但是 ? 似乎只适用于 where 子句。

我需要将模式作为参数传递并动态使用,因为它会根据 dev/cat/prod 更改

是否没有某种方法可以在 birt xml 外部生成 sql 并以某种方式注入它?

我对该主题进行了更多搜索并找到了此解决方案

<method name="beforeOpen"><![CDATA[this.queryText = "SELECT CURRENT DATE AS DATE, "+
"(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'"+
"      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'"+
"      END) AS DAYOFWEEK"+
"FROM "+params["SCHEMA"]+".COBOL_CALENDAR"+
" WHERE SERVICE_DATE = CURRENT DATE";]]></method>

但是,无论有多少关于此问题的示例以这种方式注入 sql 只会产生以下错误。

ReportDesign (id = 1): 
+ Cannot get the result set metadata.
SQL statement does not return a ResultSet object.
SQL error #1: [IBM][CLI Driver][DB2] SQL0104N  An unexpected token "SCHEMANAME" was found following "".  Expected tokens may include:  ", FROM INTO".  SQLSTATE=42601

我什至尝试reportContext.getParameterValue("SCHEMANAME")了同样结果的路线。

4

2 回答 2

2

啊,这是一个愚蠢的空格问题!

" END) AS DAYOFWEEK"+ "FROM "+params["SCHEMA"]+".COBOL_CALENDAR"+

在 FROM 前面添加一个空格可以修复它。

刘海头在液晶显示器上。

于 2009-03-03T19:00:54.637 回答
0

看起来您编辑了报告的 XML 源。这是在 SQL 查询中替换任意字符串的更图形化方法:

将您的查询编写如下:

SELECT CURRENT DATE AS DATE, 
(CASE WHEN DAYOFWEEK(CURRENT DATE) = 1 THEN 'SUNDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 2 THEN 'MONDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 3 THEN 'TUESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 4 THEN 'WEDNESDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 5 THEN 'THURSDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 6 THEN 'FRIDAY'
      WHEN DAYOFWEEK(CURRENT DATE) = 7 THEN 'SATURDAY'
      END) AS DAYOFWEEK
FROM dev.COBOL_CALENDAR
 WHERE SERVICE_DATE = CURRENT DATE

如果dev是一个有效的模式,您现在可以选择元数据集并且仍然可以用报告参数替换它。

接下来单击您的数据集并选择“脚本”选项卡。在这里选择“beforeOpen”并输入替换脚本:

this.queryText = this.queryText.replace("dev", params["SCHEMA"].value);

这样,您可以在执行查询之前将dev查询文本中的字符串替换为 Parameter 的值。SCHEMA您可以替换您想要的每个字符串(**?**来自您的问题的链接,但首先使用有效的模式,您可以在设计阶段使用元数据集。 在此处输入图像描述

于 2016-04-01T09:32:47.020 回答