如何在Oracle SQL中将多个值作为单个参数传递

时间:2019-11-08 13:15:24

标签: sql string oracle plsql

我正在尝试更新具有相同表名的多个模式中的值。

因此,我尝试创建查询以将多个值作为单个参数传递,但它不起作用。请建议我。

DECLARE
    v_sql varchar2(500);
    v_schema varchar(30);
    v_prj_id varchar2(4000);
    in_PRJ_ID varchar2(4000);
    v_prj_id := 'B00781728,B00781628,B00781611,A43670001';

    CURSOR c1 is
      SELECT v_prj_id from DUAL;

BEGIN

    OPEN c1;

    FOR i IN (SELECT trim(regexp_substr(v_prj_id, '[^,]+', 1, LEVEL)) l FROM dual CONNECT BY LEVEL <= regexp_count(v_prj_id, ',') + 1 ) LOOP

        FETCH c1 INTO in_PRJ_ID;

        EXIT WHEN c1%NOTFOUND;

        v_sql := 'UPDATE ' || in_PRJ_ID || '.SI_Recipient set email = email ||'';Seshadri.K@iconplc.com'''  ;

        EXECUTE IMMEDIATE v_sql;

    END LOOP;

    CLOSE c1;

END;

2 个答案:

答案 0 :(得分:1)

使用集合或VARRAY作为值,这样就不必拆分定界字符串,并且可以将bind参数用作要附加到电子邮件字符串的值:

DECLARE
  v_sql     varchar2(500);
  v_schema  varchar(30);
  v_prj_id  varchar2(4000);
  in_PRJ_ID SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST( 'B00781728','B00781628','B00781611','A43670001' );
BEGIN
  FOR i IN 1 .. in_PRJ_ID.COUNT LOOP
    v_sql := 'UPDATE ' || in_PRJ_ID(i) || '.SI_Recipient'
             || ' set email = email || '';'' || :addr';
    BEGIN
      EXECUTE IMMEDIATE v_sql USING 'Seshadri.K@iconplc.com';
    EXCEPTION
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE( SQLERRM );
    END;
  END LOOP;
END;
/

输出:

UPDATE B00781728.SI_Recipient set email = email || ';' || :addr
ORA-00942: table or view does not exist
UPDATE B00781628.SI_Recipient set email = email || ';' || :addr
ORA-00942: table or view does not exist
UPDATE B00781611.SI_Recipient set email = email || ';' || :addr
ORA-00942: table or view does not exist
UPDATE A43670001.SI_Recipient set email = email || ';' || :addr
ORA-00942: table or view does not exist

db <>提琴here

答案 1 :(得分:0)

这有效:

Declare
  v_sql varchar2(500);
  v_schema varchar(30);
  v_prj_id varchar2(4000):='B00781728,B00781628,B00781611,A43670001';
BEGIN
  Dbms_Output.Put_Line('v_prj_id='||v_prj_id);
  --
  FOR i IN (SELECT trim(regexp_substr(v_prj_id, '[^,]+', 1, LEVEL)) l
            FROM dual 
            CONNECT BY LEVEL <= regexp_count(v_prj_id, ',') + 1 
           ) LOOP
      --
      Dbms_Output.Put_Line('---------------------');
      --
      --
      v_sql := 'UPDATE ' || i.l|| '.SI_Recipient set email = email ||'';Seshadri.K@iconplc.com'''  ;
      --
      Dbms_Output.Put_Line('v_sql='||v_sql);
      --
      begin
        EXECUTE IMMEDIATE v_sql;
      Exception
        when others then
             Dbms_Output.Put_Line('sqlerrm='||sqlerrm);
      End;
      --
  END LOOP;
END;
/

输出为:

v_prj_id=B00781728,B00781628,B00781611,A43670001
---------------------
v_sql=UPDATE B00781728.SI_Recipient set email = email ||';Seshadri.K@iconplc.com'
sqlerrm=ORA-00942: la tabla o vista no existe
---------------------
v_sql=UPDATE B00781628.SI_Recipient set email = email ||';Seshadri.K@iconplc.com'
sqlerrm=ORA-00942: la tabla o vista no existe
---------------------
v_sql=UPDATE B00781611.SI_Recipient set email = email ||';Seshadri.K@iconplc.com'
sqlerrm=ORA-00942: la tabla o vista no existe
---------------------
v_sql=UPDATE A43670001.SI_Recipient set email = email ||';Seshadri.K@iconplc.com'
sqlerrm=ORA-00942: la tabla o vista no existe
Total execution time 344 ms

因为在我的oracle实例中我没有架构B00781728,B00781628,B00781611,A43670001,所以我收到错误ORA-00942表或视图不存在。