如何在Oracle Select语句中使用表类型

时间:2018-10-11 06:15:16

标签: oracle

我已经在Oracle中创建了以下类型

create or replace TYPE "T_STRING_LIST" 
AS TABLE OF varchar2(4000)

并使用它来收集员工ID。

V_EMP_ID_LIST T_STRING_LIST := T_STRING_LIST();


SELECT employee_id bulk collect
        INTO V_EMP_ID_LIST 
        FROM EMPLOYEE
        WHERE DEPT_ID = 101;

现在,我想在select语句中使用此V_EMP_ID_LIST从另一个表中选择详细信息。

select * from employee_address
where employee_id in (V_EMP_ID_LIST);

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

使用MEMBER OF运算符:

DECLARE
  V_EMP_ID_LIST T_STRING_LIST;
  cur           SYS_REFCURSOR;
BEGIN
  SELECT employee_id
  BULK COLLECT INTO V_EMP_ID_LIST 
  FROM   EMPLOYEE
  WHERE  DEPT_ID = 101;

  OPEN cur FOR
    select *
    from   employee_address
    where employee_id MEMBER OF V_EMP_ID_LIST;
END;
/

或表集合表达式:

DECLARE
  V_EMP_ID_LIST T_STRING_LIST;
  cur           SYS_REFCURSOR;
BEGIN
  SELECT employee_id
  BULK COLLECT INTO V_EMP_ID_LIST 
  FROM   EMPLOYEE
  WHERE  DEPT_ID = 101;

  OPEN cur FOR
    select *
    from   employee_address
    where employee_id IN ( SELECT COLUMN_VALUE FROM TABLE( V_EMP_ID_LIST ) );
END;
/