Example of How to use cursor in Oracle sqlplus,and optimizer hints is used also:
sql> CREATE TABLE TESTTAB( 2 A NUMBER,3 B CHAR(4),4 C VARCHAR2(20),5 CONSTRAINT PK_TESTTAB PRIMARY KEY (A)); Table created. sql> INSERT INTO TESTTAB VALUES(3,'CCCC','ccccc'); 1 row created. sql> INSERT INTO TESTTAB VALUES(1,'AAAA','aaaaa'); 1 row created. sql> INSERT INTO TESTTAB VALUES(2,'BBBB','bbbbb'); 1 row created. sql> SELECT * FROM TESTTAB; A B C ---------- ---- -------------------- 3 CCCC ccccc 1 AAAA aaaaa 2 BBBB bbbbb sql> SET SERVEROUTPUT ON; sql> DECLARE 2 TESTTAB_REC TESTTAB%ROWTYPE; 3 CURSOR C_TESTTAB IS SELECT /*+ INDEX_DESC(TESTTAB PK_TESTTAB) */ * FROM TESTTAB WHERE A >= 2; 4 BEGIN 5 OPEN C_TESTTAB; 6 FETCH C_TESTTAB INTO TESTTAB_REC; 7 DBMS_OUTPUT.PUT_LINE (TO_CHAR(TESTTAB_REC.A) || ' ' || TESTTAB_REC.B || ' ' || TESTTAB_REC.C); 8 FETCH C_TESTTAB INTO TESTTAB_REC; 9 DBMS_OUTPUT.PUT_LINE (TO_CHAR(TESTTAB_REC.A) || ' ' || TESTTAB_REC.B || ' ' || TESTTAB_REC.C); 10 CLOSE C_TESTTAB; 11 END; 12 / 3 CCCC ccccc 2 BBBB bbbbb PL/sql procedure successfully completed. sql> DECLARE 2 V_A TESTTAB.A%TYPE; 3 V_B TESTTAB.B%TYPE; 4 V_C TESTTAB.C%TYPE; 5 CURSOR C_TESTTAB IS SELECT /*+ INDEX_ASC(TESTTAB PK_TESTTAB) */ A,B,C FROM TESTTAB; 6 BEGIN 7 OPEN C_TESTTAB; 8 LOOP 9 FETCH C_TESTTAB INTO V_A,V_B,V_C; 10 EXIT WHEN C_TESTTAB%NOTFOUND; 11 DBMS_OUTPUT.PUT_LINE(TO_CHAR(V_A) || ' ' || V_B || ' ' || V_C); 12 END LOOP; 13 CLOSE C_TESTTAB; 14 END; 15 / 1 AAAA aaaaa 2 BBBB bbbbb 3 CCCC ccccc PL/sql procedure successfully completed.
The End原文链接:https://www.f2er.com/oracle/212235.html