Oracle:在XMLForest中调用一个函数

时间:2016-01-09 23:05:07

标签: sql xml oracle oracle-sqldeveloper

我想从函数get_phone(id)获取一个值,我得到错误" ORA-06572函数get_phone有参数"。我想知道如何在XMLForest命令中调用函数?

select XMLElement ("item", XMLForest(a.address AS "address",
                                      get_phone(id) AS "mobile") AS "item") from table_example a;

function get_phone:

CREATE function get_phone (id IN NUMBER) return NUMBER IS

mobile := NUMBER;

BEGIN
    select emp.mobile
    into mobile
    from employees emp
    where emp.identifier=id;

return mobile;

end get_phone;

我已经尝试mobile:= get_phone(id) select,但它不起作用。 你能帮我吗?

1 个答案:

答案 0 :(得分:0)

该功能无效。

  • 没有BEGIN声明。
  • mobile的定义无效。

尝试类似:

CREATE function get_phone (
  id IN NUMBER
) return NUMBER
IS
  mobile NUMBER;
BEGIN
  select emp.mobile
  into   mobile
  from   employees emp
  where  emp.identifier=id;

  return mobile;
end get_phone;
/

在你解决之后它应该工作:

select XMLElement (
         "item",
         XMLForest(
           'address' AS "address",
           get_phone(1) AS "mobile"
         )
       )
from dual;

更新 - 从表格中选择而不是DUAL

CREATE TABLE Employees ( mobile, identifier ) AS
SELECT 1234567, 1 FROM DUAL UNION ALL
SELECT 9876543, 2 FROM DUAL;

select XMLElement (
         "item",
         XMLForest(
           'address' AS "address",
           get_phone(identifier) AS "mobile"
         )
       )
from   Employees;

<强>结果:

XMLELEMENT("ITEM",XMLFOREST('ADDRESS'AS"ADDRESS",GET_PHONE(IDENTIFIER)AS"MOBILE"))
----------------------------------------------------------------------------------
<item><address>address</address><mobile>1234567</mobile></item>
<item><address>address</address><mobile>9876543</mobile></item>