我正在尝试编写plsql函数,该函数将employee_list(雇员列表)和雇员的姓名(varchar2类型)作为参数,然后遍历雇员列表,直到找到具有相应名称的雇员。找到对象后,函数将返回员工的地址。
EMPLOYEE_OBJ类型
CREATE OR REPLACE TYPE EMPLOYEE_OBJ AS OBJECT
(name varchar2(1000),
address varchar2(1000)
);
EMPLOYEE_OBJ_ARRAY类型(EMPLOYEE_OBJ的数组)
CREATE OR REPLACE TYPE EMPLOYEE_OBJ_ARRAY AS VARRAY(100) OF EMPLOYEE_OBJ;
获取对象值的函数
create or replace FUNCTION GET_EMPLOYEE_ADDRESS (employee_list EMPLOYEE_OBJ_ARRAY,name varchar2)
RETURN VARCHAR2
IS
BEGIN
FOR i in 1 .. employee_list.count LOOP
IF employee_list(i).name = name THEN
return employee_list(i).address;
END IF;
END LOOP;
RETURN NULL;
END;
我得到的错误是:
ORA-24344: success with compilation error
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_190100", line 590
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_190100", line 576
ORA-06512: at "APEX_190100.WWV_FLOW_DYNAMIC_EXEC", line 2033
您能帮我解决此问题吗? 还有没有更简单的方法可以做到这一点?就像选择查询一样。
答案 0 :(得分:1)
尽管提供的printPriv(D d)
在至少两个版本的Oracle上似乎可以编译并执行良好,但是有一种方法可以检索地址而无需遍历集合。下面是使用您提供的类型的示例:
FUNCTION
查询:
CREATE OR REPLACE TYPE EMPLOYEE_OBJ AS OBJECT
(name varchar2(1000),
address varchar2(1000)
);
CREATE OR REPLACE TYPE EMPLOYEE_OBJ_ARRAY AS VARRAY(100) OF EMPLOYEE_OBJ;
结果:
SELECT ADDRESS FROM TABLE (EMPLOYEE_OBJ_ARRAY(
EMPLOYEE_OBJ('John','Finland'),
EMPLOYEE_OBJ('Molly','Spain')))
WHERE NAME = 'John';