转换与','连接的字符串在oracle SQL的子查询中使用

时间:2017-01-30 06:39:05

标签: sql oracle plsql

我想使用我的功能结果,例如'S500,S600,S700,S800'在另一个脚本的子查询中,如:

where dept_no in (my result of function)

所以我想将我的字符串结果转换为('S500','S600','S700','S800')

我尝试使用动态SQL执行此操作,但我无法使其工作。

4 个答案:

答案 0 :(得分:0)

希望在snipet之下满足你的要求。

{y: 34, color: '#66ff33'}

答案 1 :(得分:0)

你需要一个迭代器和用逗号分隔文本:

select empno,ename,sal,deptno
from emp
where empno in (
select to_number(
rtrim(
substr(emps,
instr(emps,',',1,iter.pos)+1,
instr(emps,',',1,iter.pos+1) -
instr(emps,',',1,iter.pos)),',')) emps
from (select ','||'7654,7698,7782,7788'||',' emps from t1) csv,
(select rownum pos from emp) iter
where iter.pos <= ((length(csv.emps) -
length(replace(csv.emps,',')))/length(','))-1
) 

但最好重写你的函数来返回光标。

答案 2 :(得分:0)

你可以使用收藏:

SELECT *
  FROM YOUR_TABLE
 WHERE DEPT_NO IN (SELECT *
                     FROM TABLE (SPLIT ('S500,S600,S700,S800')))--splits text with comma, for other chars use split(text, split_char)

使用会员

SELECT *
  FROM YOUR_TABLE
 WHERE DEPT_NO MEMBER OF SPLIT ('S500,S600,S700,S800')--splits text with comma, for other chars use split(text, split_char)

分裂功能是:

CREATE OR REPLACE TYPE SPLIT_TBL AS TABLE OF VARCHAR2 (32767);

CREATE OR REPLACE FUNCTION SPLIT (P_LIST VARCHAR2, P_DEL VARCHAR2 := ',')
   RETURN SPLIT_TBL
   PIPELINED
IS
   L_IDX     PLS_INTEGER;
   L_LIST    VARCHAR2 (32767) := P_LIST;
BEGIN
   LOOP
      L_IDX := INSTR (L_LIST, P_DEL);

      IF L_IDX > 0
      THEN
         PIPE ROW (SUBSTR (L_LIST, 1, L_IDX - 1));
         L_LIST := SUBSTR (L_LIST, L_IDX + LENGTH (P_DEL));
      ELSE
         PIPE ROW (L_LIST);
         EXIT;
      END IF;
   END LOOP;

   RETURN;
END SPLIT;

答案 3 :(得分:0)

 FUNCTION GET_TS_EACH_DAY_DEPARTMENT (P_SER_NO     VARCHAR2,
                                   P_TS_DATE    DATE
                                   )
      RETURN STRING_TABLE     
   IS
     V_DEPT_NO       VARCHAR2 (4000);
     V_DEPT          VARCHAR2(4000);
     V_TABLE         STRING_TABLE:=STRING_TABLE();
     J NUMBER:=1;
   BEGIN
     for i in (select distinct ts_day dayy from WEB_TS_USER_LOCATIONS_V ) loop
     V_TABLE.EXTEND;
        V_TABLE(J):= WEB_TS_PKG.GET_TS_DAY_DEPARTMENT (P_SER_NO ,P_TS_DATE  , i.dayy );
         J:=J+1;
     end loop;
    RETURN V_TABLE;
   END GET_TS_EACH_DAY_DEPARTMENT;