嵌套表和EXECUTE IMMEDIATE

时间:2014-11-18 15:33:05

标签: plsql oracle11g dynamic-sql

我从PL / SQL过程中剪切了下面的问题 我需要变量来创建动态SQL语句,但我还需要嵌套表。

 l_code list_code := list_code();

 l_code := PKG_DATA.GET_CODES(in_code) ;


v_sql := 'SELECT ' || in_var_name ||'
              FROM view 
              WHERE supplier = '||in_supplier ||'
                AND factory = '|| in_factory ||'
                AND code IN ('|| ( SELECT * FROM TABLE(l_code) )||')  ';


 EXECUTE IMMEDIATE   v_sql
                INTO v_value;

v_value是一个嵌套表

  

[错误] PLS-00103(60:46):PLS-00103:遇到符号“SELECT”   期待以下之一:

     

( - + case mod new not null continue,[Error] PLS-00103   (60:74):PLS-00103:当期待其中一个时遇到符号“)”   以下内容:

     

,;对于   作为具有交叉减号的组开始联合wh

有没有人有想法或剪断?

thx

2 个答案:

答案 0 :(得分:0)

我建议使用绑定变量

v_sql := 'SELECT :in_var_name
          FROM view 
          WHERE supplier = :in_supplier
            AND factory = :in_factory
            AND code IN (SELECT * FROM TABLE(:l_code) ))  ';

EXECUTE IMMEDIATE v_sql
INTO v_value
USING in_var_name, in_supplier, in_factory, l_code;

但可能的是,您根本不需要动态SQL:

SELECT in_var_name
INTO v_value
FROM view
WHERE supplier = in_supplier
  AND factory = in_factory
  AND code IN (SELECT * FROM TABLE(l_code));

答案 1 :(得分:0)

v_sql := 'SELECT ' || in_var_name 
       || ' FROM view ' 
       || ' WHERE supplier = :supplier AND factory = :factory ' 
       || '   AND code IN (SELECT * FROM TABLE(PKG_DATA.GET_CODES(:code)))';

EXECUTE IMMEDIATE v_sql INTO v_value USING in_supplier, in_factory, in_code;