rowtype有多行?

时间:2015-04-16 08:35:03

标签: oracle plsql

我必须做一个pl / sql,它显示了一些sql的行。对不起,我是新手,我正在尝试使用rowtype,但我看到只显示结果的第一行。如果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;
/

3 个答案:

答案 0 :(得分:1)

  

我尝试使用rowtype,但我发现只显示结果的第一行。

来自documentation

  

%ROWTYPE属性提供表示数据库表中一行的记录类型。记录可以存储整行数据   从表中选择或从游标或游标变量中提取。

来到你的问题,

  

如果sql语句结果有多行,那么将sql语句结果存储到某个变量的最佳方法是什么?

您需要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>

答案 1 :(得分:0)

这就是你要找的东西吗?

  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

答案 2 :(得分:0)

请检查一下:

    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;

    /