Oracle cursor in sqlplus, and optimizer hints

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值