批量收集到两个或更多变量

时间:2017-09-09 09:08:16

标签: oracle plsql

我希望在循环游标时使用Bulk collect获取三个变量。可能吗?请参阅下面的查询(假设变量已声明)

DECLARE
    CURSOR recCurosr
    IS
        SELECT t1.name,t2.value1, t2.value2
        FROM   table1 t1, table2 t2
        WHERE  t1.pk = t2.pk;

BEGIN
    OPEN recCurosr;
    LOOP
        FETCH recCurosr BULK COLLECT INTO var1, var2, var3;
        BEGIN
            FORALL i IN 1 .. var1.count SAVE EXCEPTIONS
                UPDATE table1
                SET    value1= var2,
                WHERE  value1= var3
                AND    pk = var1;

1 个答案:

答案 0 :(得分:1)

如果光标从您需要的差分表中选择数据:

DECLARE
   CURSOR c1 IS SELECT 'name1', 1, 2 FROM dual;

   TYPE abc IS RECORD ( -- define a record type record 'abc'
      name      VARCHAR2 (100),
      value1    NUMBER,
      value2    NUMBER
   );
   myAbc       abc; -- declare an object with type 'abc'

   TYPE abcTab IS TABLE OF abc; -- define a table type 'abcTab'
   myAbcTab    abcTab; -- declare an object with type 'abcTab'

BEGIN
   OPEN c1;
   FETCH c1 BULK COLLECT INTO myAbcTab;
   FOR i IN myAbcTab.FIRST .. myAbcTab.LAST LOOP
     dbms_output.put_line(myAbcTab(i).name);
   END LOOP;
   CLOSE c1;
END;
/

如果您只需要单个表格中的所有列,则可以使用myAbcTab table1%ROWTYPE;代替