在oracle中怎么用静态游标,『ORACLE』 PLSQL静态游标的使用(11g)

『ORACLE』 PLSQL静态游标的使用(11g)

『ORACLE』 PLSQL静态游标的使用(11g)

#静态游标指的是程序执行的时候不需要再去解析sql语言,对于sql语句的解析在编译的时候就可以完成的。

动态游标由于含有参数,对于sql语句的解析必须要等到参数确定的时候才能完成。

从这个角度来说,静态游标的效率也比动态游标更高一些。

#游标的相关概念:

定义:

游标它是一个服务器端的存储区,这个区域提供给用户使用,在这个区域里

存储的是用户通过一个查询语句得到的结果集,用户通过控制这个游标区域当中

的指针 来提取游标中的数据,然后来进行操作。

实质:

是用户在远程客户端上对服务器内存区域的操作,由数据库为用户提供这样的

一个指针,使得用户能够去检索服务器内存区的数据。

#游标具有的属性:

1、%ISOPEN(确定游标是否已经打开 true or false)

2、%FOUND(返回是否从结果集提取到了数据 true or false)

3、%NOTFOUND(返回是否从结果集没有提取到数据 true or false)

4、%ROWCOUNT(返回到当前为止已经提取到的实际行数)

#游标分类

一、静态游标

1、隐式游标:

对于select..into...语句,一次只能从数据库中获取到一条数据,对于这种类型的DML SQL语句,就是隐式cursor

select update/insert/delete操作

2、显示游标:

由程序员定义和管理,对于从数据库中提取多行数据,就需要使用显式cursor

1)定义游标---cursor  [cursor name]  is

2)打开游标---open    [cursor name]

3)操作数据---fetch    [cursor name]

4)关闭游标---close    [cursor name]

二、REF游标

aa98eb91cbb43715ef36e52dd58078d2.png

使用显示游标前必须首先定义游标declare cursor

打开游标open cursor_name

提取数据fetch cursor_name into variable1,variable2...(一次提取一条记录); fetch cursor_name bulk collect into collect1,collect2...;(一次提取多条记录)

关闭游标close cursor_name

显示游标示例

SQL> declare

2 cursor c_emp_cursor is

3 select empno,ename from emp where deptno =30;

4 v_empno emp.empno%type;

5 v_lname emp.ename%type;

6 begin

7 open c_emp_cursor;

8 loop

9 fetch c_emp_cursor

10 into v_empno, v_lname;

11 exit when c_emp_cursor%notfound;

12 dbms_output.put_line(v_empno || ' ' || v_lname);

13 end loop;

14 close c_emp_cursor;

15 end;

16 /

7499 ALLEN

7521 WARD

7654 MARTIN

7698 BLAKE

7844 TURNER

7900 JAMES

PL/SQL procedure successfully completed.

参数游标

SQL> declare

2 cursor c_emp_cursor(v_dept_id number) is

3 select empno,ename

4 from emp

5 where deptno =v_dept_id;

6 v_empno emp.empno%type;

7 v_lname emp.ename%type;

8 begin

9 open c_emp_cursor(30);

10 loop

11 fetch c_emp_cursor

12 into v_empno, v_lname;

13 exit when c_emp_cursor%notfound;

14 dbms_output.put_line(v_empno || ' ' || v_lname);

15 end loop;

16 close c_emp_cursor;

17 end;

18 /

7499 ALLEN

7521 WARD

7654 MARTIN

7698 BLAKE

7844 TURNER

7900 JAMES

PL/SQL procedure successfully completed.

隐式游标(FOR循环)

oracle内部会隐含打开、提取和关闭游标

游标型FOR循环是和一个现实游标或者直接放在循环边界中的SELECT语句关联在一起的循环。如果你需要取出游标的每条记录依次处理,就可以使用游标FOR循环,而这种也是游标的主要用法。语法如下:

FOR record IN {cursor_name | select statement}

LOOP

executable statements

END LOOP;

其中record是一个记录,这个记录是PL/SQL根据cursor_name这个游标%ROWTYPE属性隐式声明出来的。注意:不要现实的声明一个与循环索引(record)同名的记录,会导致逻辑错误。直接在循环中嵌套SELECT语句。

SQL> begin

2 for emp_record in

3 (select empno, ename from emp

4 where deptno = 30 ) loop

5 dbms_output.put_line(emp_record.empno || ' ' ||

6 emp_record.ename);

7 end loop;

8 end;

9 /

7499 ALLEN

7521 WARD

7654 MARTIN

7698 BLAKE

7844 TURNER

7900 JAMES

PL/SQL procedure successfully completed.

『ORACLE』 PLSQL静态游标的使用(11g)相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值