oracle cursor

In the PL/SQL code block, you can only use the select statement for assignment select...into...from...., you cannot query a result set 
(if you need a table, please move the mouse to the bottom, there is a table creation code) 
(1). Implicit cursor

begin
     dbms_output.put_line('执行之前,影响的行数:'||SQL%ROWCOUNT);
     delete from orderinfo where orderid=20160002; dbms_output.put_line('执行之后,影响的行数:'||SQL%ROWCOUNT); end;

(2). Display the cursor: specify the query statement when defining the cursor

     CURSOR  游标名  IS  操作语句;   --1、定义游标 

     OPEN   游标名;  --2、打开游标
     LOOP FETCH 游标名 INTO 变量名; --3.读取游标 EXIT WHEN 游标名%NOTFOUND; 处理语句; END LOOP; CLOSE 游标名;--4.关闭游标

    create or replace procedure proc1 as cursor cur_user is select * from userinfo; --1.定义游标 theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 begin --2.打开游标 open cur_user; LOOP --3.获取游标的下一行数据 FETCH CUR_USER INTO theUser; --游标没有读到数据退出循环 exit when CUR_USER%notfound; ---游标读到数据就输出 dbms_output.put_line(theUser.userId||' '||theUser.userName); END LOOP; dbms_output.put_line('数据备份完毕!'); --4.关闭游标 close cur_user; end;

Example 2:

create or replace procedure proc1(upoint int) as cursor cur_user is select * from userinfo where userPoint>upoint; --1.定义游标 theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 begin --2.打开游标 open cur_user; LOOP --3.获取游标的下一行数据 FETCH CUR_USER INTO theUser; --游标没有读到数据退出循环 exit when CUR_USER%notfound; ---游标读到数据就输出 dbms_output.put_line(theUser.userId||' '||theUser.userName); END LOOP; dbms_output.put_line('数据备份完毕!'); --4.关闭游标 close cur_user; end; call proc1(0);

 

Example 3: Cursor with parameters

create or replace procedure proc1 as cursor cur_user(upoint int) is select * from userinfo where userPoint>upoint; --1.定义游标 theUser userinfo%rowtype; --定义变量与userinfo数据行映射,用于存储游标读取到的数据 begin --2.打开游标 open cur_user(2); LOOP --3.获取游标的下一行数据 FETCH CUR_USER INTO theUser; --游标没有读到数据退出循环 exit when CUR_USER%notfound; ---游标读到数据就输出 dbms_output.put_line(theUser.userId||' '||theUser.userName); END LOOP; dbms_output.put_line('数据备份完毕!'); --4.关闭游标 close cur_user; end; call proc1();

Example 4: for loop to read cursor

create or replace procedure proc1 as cursor cur_user is select * from userinfo; --1.定义游标 begin for theUser in cur_user loop dbms_output.put_line(theUser.userId||' '||theUser.userName); end loop; dbms_output.put_line('数据备份完毕!'); end; call proc1();

 

4. Reference cursor: no query statement is specified when defining, and a query statement is specified when opening

    TYPE    游标类型名   IS  REF   CURSOE; --定义类型
    游标变量名  游标类型名;  --使用类型定义游标变量
    OPNE   游标变量名   FOR 查询语句;

 

create or replace procedure proc1(i int) as type refType is ref cursor;--定义类型 ,类型名为refType theCursor refType;--通过类型定义游标 游标名为theCursor theUser userInfo%rowtype; thePro proInfo%rowtype; begin if i=1 then open theCursor for select * from userInfo; loop fetch theCursor into theUser; exit when theCursor%notfound; dbms_output.put_line(theUser.userId||' '||theUser.userName); end loop; close theCursor; elsif i=2 then open theCursor for select * from proInfo; loop fetch theCursor into thePro; exit when theCursor%notfound; dbms_output.put_line(thePro.proId||' '||thePro.proName); end loop; close theCursor; else dbms_output.put_line('选择错误!'); end if; end proc1; call proc1(2);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021613&siteId=291194637