首页 > 编程知识 正文

数据库中cursor游标的概念,游标更新数据

时间:2023-05-05 02:49:50 阅读:120697 作者:770

另一方面,光标的主要对象是操作对象从数据库返回的结果集,在广义上可以看作是从结果集中一行一行地读取数据的方法、机制。 狭义上也可以理解为操作光标的结果集。 由于必须为光标指定操作的第一个操作结果集(corsor c is select ),因此也可以将其视为为操作光标的每一行创建的临时文件。

二、光标最常见的作用是搜索数据。 大多数语言支持使用游标从SQL数据库检索数据,并保存查询结果以供将来使用。 例如,如果在日常流程中需要重复处理,则选择只加载一次结果集,只创建一次游标并重复使用,这比多次创建结果集更有效率。

三、光标的四个属性(以下所有默认curcor_name均为c ) ) ) ) ) ) ) ) ) )。

1、%notfound是否在最后一个fetch后检索结果,yydggx类型,如果有结果则返回true

板栗:

开放c;

loop

fetch c into v_name;

退出when (c % not found );

DMS _ output.put _ line (v _ name.vname;

结尾环路;

关闭c;

2、%found最后一次fetch后是否获取结果,yydggx类型,如果没有,则为false

举个例子:

开放c;

while c%found loop

.

fetch c into v_name;

结尾环路;

关闭c;

3、%rowcount返回光标获取的行数为number类型

板栗:

开放c;

loop

fetch c into v_name;

退出when c % rowcount 5;

.

结尾环路;

关闭c;

4、%isopen指示当前是否打开光标,类型为yydggx,打开时为true,很少使用;

举板栗吧。 如果要确定光标是否打开,请在未打开的情况下执行打开的操作

ifnot(c%isopen ) then

开放c;

结束If;

fetch c into;

四.用光标进行简单的查询操作

注意:如果您想让cmd打印我们请求的结果,必须先添加set serveroutput on语句

declare

cursor c is (声明游标并指向以下查询语句的结果集)。

select * from emp;

v_emp c%rowtype; (生命变量,指向游标返回结果集的行)

比根

开放c; (查询语句在游标打开后执行。)

f

etch c into v_emp;   (捕捉到一行结果放到变量v_emp中)

                                dbms_output.put_line(v_emp.ename);  (打印得到的结果中的ename字段)

                        close c;

                   end; 

                    /

        五、使用loop循环的方式遍历结果集

                declare 

                        cursor c is

                                select * from emp;

                        v_emp c%rowtype;

                begin

                        open c;

                            loop

                                    fetch c into v_emp;

                                    exit when(c%notfound);

                                    dbms_output.put_line(v_emp.ename);

                            end loop;

                        close c;

                end; 

                /

        (注:如果此处将打印语句与判断何时退出的语句交换位置,会出现将最后一个查到的数据重新打印一遍的情况)

        六、使用while循环遍历得到数据

                declare

                        cursor c is

                                select * from emp;

                        v_emp c%rowtype;  //使用while遍历,仍然需要对v_emp进行声明

                begin

                        open c;

                                fetch c into v_emp;

                                while(c%found) loop   (当当前最近的一次游标发现了一行的时候执行如下语句)

                                        dbms_output.put_line(v_emp.ename);

                                        fetch c into v_emp;

                        end loop;

                        close c;

                end;

                /

    七、使用for循环遍历数据

                declare

                        corsor c is 

                                select * from emp;

                begin

                        for v_emp in c loop

                                dbms_output.put_line(v_emp.ename);

                        end loop;

                end;

    八、带参数的游标

                declare

                        cursor c(v_deptno emp.deptno%type,v_job emp.job%type)

                        is

                                select ename,sal from emp where deptno = v_deptno and job = v_job;

                    begin

                            for v_temp in c(30,'CLERK') loop

                                    dbms_output.put_line(v_temp.ename);

                            end loop;

                    end;

                    /

    九、可更新的游标

            declare

                    cursor c is

                            select * from emp for update;

             begin

                        for v_temp in c loop;

                                if(v_temp.sal < 2000) then

                                        update emp set sal = sal*2 where current of c(指当前游标);

                                 elsif (v_temp.sal = 5000) then

                                        delete from emp where current of c;

                                  end if;

                         end loop;

                         commit;

                end;

                     

                

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。