rowtype 有多于一行?

rowtype with more than one row?

我必须做一个 pl/sql 来显示 sql 的一些行。抱歉,我是新手,我正在尝试使用行类型,但我看到它只显示结果的第一行。如果 sql 语句结果有多于一行,最好的方法是如何将其存储到某个变量中。 谢谢,对不起我的英语!

    BEGIN
FOR I IN (select xxxxxxxxxxxxx) LOOP    
    DECLARE
    CURSOR CURSORMINUS 
    IS
            select * xxxxxxx
            minus
            select * xxxxxxxxx;

            v_reg CURSORMINUS%rowtype;
      BEGIN
            OPEN CURSORMINUS;
            FETCH CURSORMINUS INTO V_REG;



            IF CURSORMINUS%NOTFOUND THEN DBMS_OUTPUT.PUT_LINE('no differences...'); 
            ELSE
            DBMS_OUTPUT.PUT_LINE('the differences are ....' || v_reg.APP_OBJECT );
            END IF;

      END;

END LOOP;
END;
/

I'm trying to do with rowtype but I see that only shows me the first line of the result.

来自 documentation,

The %ROWTYPE attribute provides a record type that represents a row in a database table. The record can store an entire row of data selected from the table or fetched from a cursor or cursor variable.

回答你的问题,

How is the best way to store the sql statement result into some variable if it has more than one row.

您需要 PL/SQL 集合类型。

例如,

SQL> set serveroutput on
SQL> DECLARE
  2  TYPE tbl_emp
  3  IS
  4    TABLE OF emp%ROWTYPE;
  5    l_tab tbl_emp;
  6  BEGIN
  7    SELECT * BULK COLLECT INTO l_tab FROM emp;
  8    FOR i IN 1..l_tab.count
  9    LOOP
 10      dbms_output.put_line('Empno = '||l_tab(i).empno);
 11    END LOOP;
 12  END;
 13  /
Empno = 7369
Empno = 7499
Empno = 7521
Empno = 7566
Empno = 7654
Empno = 7698
Empno = 7782
Empno = 7788
Empno = 7839
Empno = 7844
Empno = 7876
Empno = 7900
Empno = 7902
Empno = 7934

PL/SQL procedure successfully completed.

SQL>

这是您要找的吗?

  SCOTT@research 16-APR-15> select * from test1;

  VAL1       VAL2       VAL3
  ---------- ---------- ----------
   555          2          4
     3          2          4
   123          2          3
    42          3

   declare
   begin
   for i in (select * from test1) loop
   dbms_output.put_line('output is' );
   dbms_output.put_line(i.val1);

   end loop;
   end;
   /

output is   
555
3
123
42

请检查:

    BEGIN
        FOR I IN (select xxxxxxxxxxxxx) LOOP    
            DECLARE
            has_records BOOLEAN:=FALSE;
            CURSOR CURSORMINUS 
            IS
                    select * xxxxxxx
                    minus
                    select * xxxxxxxxx;

                    v_reg CURSORMINUS%rowtype;
              BEGIN
                    OPEN CURSORMINUS;
                    LOOP 
                        FETCH CURSORMINUS INTO V_REG;
                        EXIT WHEN CURSORMINUS%NOTFOUND;
                        DBMS_OUTPUT.PUT_LINE('the differences are ....' || v_reg.APP_OBJECT );
                        has_records:=TRUE;


                    END LOOP;
                    CLOSE CURSORMINUS;

                    IF NOT has_records  THEN 
                        DBMS_OUTPUT.PUT_LINE('no differences...'); 
                    END IF;

              END;

        END LOOP;
        END;

    /