如何将String传递给sql来执行并生成输出到外部查询

时间:2014-11-10 07:50:43

标签: oracle

我在oracle存储过程中有一个变量

GDFCID_STRING2 VARCHAR2(1000);
GDFCID_STRING := ' select regexp_substr(''1005771621,1001035181'',''[^,]+'', 1, level)         from dual
connect by regexp_substr(''1005771621,1001035181'', ''[^,]+'', 1, level) is not null ';

我想使用该变量在IN子句中执行相同的查询,如this-> 在存储过程中,我有一个sql->

select * from xyz where gfcid in
(--here I want to execute the above query (GDFCID_STRING) 
and feed the output of query as input to IN clause--)

1 个答案:

答案 0 :(得分:0)

只需将查询放入IN clause本身即可。无需动态执行它。

SELECT * 
FROM   xyz 
WHERE  gfcid IN (SELECT Regexp_substr('1005771621,1001035181', '[^,]+', 1, LEVEL 
                        ) 
                 FROM   dual 
                 CONNECT BY Regexp_substr('1005771621,1001035181', '[^,]+', 1, 
                            LEVEL) 
                            IS NOT 
                            NULL)

例如,让我们看一下使用EMP表的测试用例:

SQL> SELECT empno,
  2         ename,
  3         deptno
  4  FROM   emp
  5  WHERE  ename IN
  6         (
  7            SELECT Regexp_substr('SMITH,SCOTT','[^,]+', 1, LEVEL)
  8            FROM   dual
  9            CONNECT BY Regexp_substr('SMITH,SCOTT', '[^,]+', 1, LEVEL) IS NOT NULL)
 10  /

     EMPNO ENAME          DEPTNO
---------- ---------- ----------
      7369 SMITH              20
      7788 SCOTT              20

SQL>