0

我正在尝试使用流水线函数来节省时间并减少查询中的冗余。有问题的函数根据一些输入从参考表中返回数据。我从中选择的主数据表中的记录有多个列,它们都引用了参考表。我遇到的问题是,当我尝试在查询中多次使用流水线函数时,出现“光标已打开”错误。

例如:

select xmlelement("genInf", xmlelement("ID", vt.ID),
                           xmlelement("vID", vt.V_ID),
                           xmlelement("vNum", vt.V_NUM),
                           xmlelement("terrDataCode", TERR_CODE.column_value), --data is based on reference table
                           xmlelement("ABValCode", AB_VAL_CD.column_value), --data is based on reference table
                           ...
from V_TAB vt, table(UTIL.fn_getOvrdValXML(vt.terr_cd_id)) TERR_CODE,
               table(UTIL.fn_getOvrdValXML(vt.ab_val_id)) AB_VAL_CD
where vt.ID = in_vID;

这工作正常,直到我添加了对管道函数 (fn_getOvrdValXML) 的第二个引用,我现在得到“光标已打开”错误。

流水线函数非常简单:

type t_XMLTab is table of XMLType; --this type is in the spec
....
function fn_getOvrdValXML(in_ID in ovrd.id%type) return t_XMLTab
        pipelined is
    begin
        for r in C_OvrdVal(in_ID) loop
            pipe row(r.XMLChunk);
        end loop;
        return;
    end;

游标同样简单:

cursor C_OvrdVal(in_ID in ovrd.id%type) is
        select xmlforest(ID as "valueID", S_VAL as "sValue", U_VAL as "uplValue",
                          O_VAL as "oValue", O_IND as "oIndicator", F_VAL as "finalValue",
                          O_RSN as "reason") AS XMLChunk
        from ovrd_val xov;
        where xov.id = in_ID;

有没有办法解决这个问题,或者我应该尝试以不同的方式解决这个问题(必须引用 ovrd_val 并以相同的方式多次输出 xmlforest 的问题)?

我承认我是流水线功能的新手,所以我不能 100% 确定这是一个合适的用途,但它在当时是有道理的,我对其他想法持开放态度;)

4

2 回答 2

1

如果您使用的是管道函数,那么您的最低要求是 9i,这意味着您可以使用 WITH 子句:

WITH ovrdValXML AS (
  select xov.id,
         xmlforest(ID as "valueID", S_VAL as "sValue", U_VAL as "uplValue",
                        O_VAL as "oValue", O_IND as "oIndicator", F_VAL as "finalValue",
                        O_RSN as "reason") AS XMLChunk
     from ovrd_val xov)
SELECT xmlelement("genInf", xmlelement("ID", vt.ID),
                       xmlelement("vID", vt.V_ID),
                       xmlelement("vNum", vt.V_NUM),
                       xmlelement("terrDataCode", TERR_CODE.column_value), --data is based on reference table
                       xmlelement("ABValCode", AB_VAL_CD.column_value), --data is based on reference table
                       ...
  FROM V_TAB vt
  JOIN ovrdValXML terr_code ON terr_code = vt.?
                           AND terr_code.id = vt.terr_cd_id
  JOIN ovrdValXML ab_val_cd ON ab_val_cd = vt.?
                           AND ab_val_cd.id = vt.ab_val_cd
 WHERE vt.id = IN_VID;

未经测试,也不清楚您要加入什么 - 因此?加入标准。

于 2009-12-14T22:54:12.583 回答
0

您是否尝试过在管道行之前实际关闭该管道函数内的光标?

OPEN C_OvrdVal(in_ID);
FETCH c_OrdVal INTO my_chunk_variable;
CLOSE C_OrdVal;
PIPE ROW my_chunk_variable;
RETURN;
于 2009-12-15T10:06:39.363 回答