0

我有一个包含 2 列的表 tsk:一个任务 ID 编号和一个 XMLTYPE 中的任务范围,可以像:

<scope>
    <siteCode>2</siteCode>
    <supplierCode>1111</supplierCode>
    <contractCode>464</contractCode>
    <orderNumber>85235478</orderNumber>
</scope>

但是标签下的元素可能因一条记录而异

我需要选择与范围内某些条件匹配的任务 ID。例如 :

select tskid
from tsk t 
where xmlexists('$a/scope[siteCode = 2 and supplierCode = 111]' passing t.tskscope as "a");

由于范围可能会有所不同,我有一个 PL/SQL 函数,它采用 varchar2 类型的 xml 路径 p_xmlpath 来查找。例如,p_xmlpath 将是:

p_xmlpath := 'scope[siteCode = 2 and supplierCode = 1111]';

然后我想用 XMLEXISTS 运行查询以找到匹配的记录。我想通过以下方式使用绑定变量:

select tskid
from tsk t 
where xmlexists('$a/$b' passing t.tskscope as "a", p_xmlpath as "b" );

通过这样做,查询返回所有记录,而不使用 xmlexists 条件。

有人知道如何管理这个,即有一个可变路径给 XMLEXISTS 吗?

附加信息:到目前为止,我使用了函数 existsNode 并且以下查询正确地完成了工作:

select tskid
from tsk t 
where existsnode(t.tskscope, p_xmlpath) = 1;

但一方面existsNode已被弃用,另一方面我注意到在我的情况下,函数xmlexists明显比existsNode快,这就是我要切换到xmlexists的原因。

提前致谢。

4

1 回答 1

0

我认为你不能;它似乎只允许搜索值是变量,而不是整个搜索条件。

作为一种解决方法,您可以使用以下命令在 runtome 构建 XPath replace()

select tskid
from tsk t 
where xmlexists(replace('$a/$b', '$b', p_xmlpath) passing t.tskscope as "a");

或者如果您不想在其中有替换,则将 XPath 字符串构建为变量,并包含以下$a/部分:

p_xmlpath := '$a/' || p_xmlpath;

select tskid
from tsk t 
where xmlexists(p_xmlpath passing t.tskscope as "a");
于 2017-06-16T10:53:07.870 回答