Oracle存储过程学习_游标CURSOR使用

Oracle存储过程学习_游标CURSOR使用
Oracle存储过程学习_游标CURSOR使用

游标CURSOR的使用学习

游标的类型:

1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐

式游标,名字固定叫sql。

2,显式游标:显式游标用于处理返回多行的查询。

3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询

的结果

一、隐式游标

在PL/SQL中使用DML语句时自动创建隐式游标 q隐式游标自动声明、打开和关闭,其名为 SQL q通过检查隐式游标的属性可以获得最近执行的DML 语句的信息q隐式游标的属性有:

q%FOUND – SQL 语句影响了一行或多行时为 TRUE

q%NOTFOUND – SQL 语句没有影响任何行时为TRUE

q%ROWCOUNT – SQL 语句影响的行数

q%ISOPEN - 游标是否打开,始终为FALSE

begin

update student s set s.sage = s.sage + 10;

if sql %FOUND then

dbms_output.put_line('这次更新了' || sql% rowcount);

else

dbms_output.put_line('一行也没有更新');

end if;

end;

在select中有两个中比较常见的异常: 1. NO_DATA_FOUND 2. TOO_MANY_ROWS declare

sname1 student.sname%TYPE;

begin

select sname into sname1 from student;

if sql%found then

dbms_output.put_line(sql%rowcount);

else

dbms_output.put_line('没有找到数据');

end if;

exception

when too_many_rows then

dbms_output.put_line('查找的行记录多于1行');

when no_data_found then

dbms_output.put_line('未找到匹配的行');

end;

显式游标:

sqlserver与oracle的不同之处在于:最后sqlserver会deallocate 丢弃游标,而oracle只有前面四步:声明游标、打开游标、使用游标读取记录、关闭

游标。

显式游标的使用:

----------------------------无参数游标-------------------------------declare

sname varchar2(20); --声明变量

cursor student_cursor is

select sname from student; --声明游标

begin

open student_cursor; --打开游标

fetch student_cursor

into sname; --让游标指针往下移动

while student_cursor%found--判断游标指针是否指向某行记录

loop

--遍历

dbms_output.put_line('学生姓名' || sname);

fetch student_cursor

into sname;

end loop;

close student_cursor;

end;

-----------------------------有参数游标-------------------------------declare

sname student.sname%type;

sno student.sno%type;

cursor student_cursor(input_sno number) is

select s.sname, s.sno from student s where s.sno > input_sno; --声明带参数的游标

begin

sno := &请输入学号; --要求从客户端输入参数值,"&"相当于占位符;

open student_cursor(sno); --打开游标,并且传递参数

fetch student_cursor

into sname, sno; --移动游标

while student_cursor% found loop

dbms_output.put_line('学号为:' || sno || '姓名为:' || sname);

fetch student_cursor

into sname, sno;

end loop;

close student_cursor;

end;

-----------------------------循环游标------------------------------- -- Created on 18-1月-15 by 永文

declare

stu1 student%rowtype; --这里也不需要定义变量来接收fetch到的值

cursor student_cursor is

select * from student;

begin

open student_cursor; --这里不需要开启游标

for stu1 in student_cursor loop

dbms_output.put_line('学生学号:' || stu1.sno || '学生姓名:' ||

stu1.sname);

fetch student_cursor

into stu1; --也不需要fetch了

end loop;

close student_cursor; --这里也不需要关闭游标

end;

--------------------------使用游标更新行------------------------------- declare

stu1 student%rowtype;

cursor student_cursor is

select * from student s where s.sno in (2, 3) for update; --创建更新

游标

begin

open student_cursor;

fetch student_cursor

into stu1; --移动游标

while student_cursor%found--遍历游标,判断是否指向某个值

loop

update student set sage = sage + 10where current of student_cursor; --通过游标中的信息更新数据

fetch student_cursor

into stu1; --移动游标

end loop;

close student_cursor;

end;

declare

stu1 student%rowtype;

cursor student_cursor is

select * from student s where s.sno in (2, 3) for update; --创建更新

游标

begin

open student_cursor;

-- fetch student_cursor into stu1;--移动游标

-- while student_cursor%found--遍历游标,判断是否指向某个值

loop

fetch student_cursor

into stu1; --移动游标

exit when student_cursor %notfound;

update student set sage = sage + 10where current of student_cursor; --通过游标中的信息更新数据

end loop;

close student_cursor;

end;

--------------使用fetch ... bulk collect into-------------------------- declare

cursor my_cursor is

select ename from emp where deptno = 10; --声明游标

type ename_table_type is table of varchar2(10); --定义一种表类型,表中的

属性列为varchar2类型

ename_table ename_table_type; --通过上面定义的类型来定义变量

begin

open my_cursor; --打开游标

fetch my_cursor bulk collect

into ename_table; --移动游标

for i in1 .. ename_table.count loop

dbms_output.put_line(ename_table(i));

end loop;

close my_cursor;

end;

e

---------------------显示游标题目--------------------------------------

SQL >

select * from student;

XH XM

---------- ----------

1 A

2 B

3 C

4 D

SQL >

select * from address;

XH ZZ

---------- ----------

2郑州1开封3洛阳4新乡

完成的任务 :给表student添加一列zz, 是varchar2(10) 类型;再从address中,将

zz字段的数值取出来,对应的插入到 student新增的zz列中。即:得到的结果:student

表中,是: XH XM ZZ

-- ---------- ------

1 A 开封

2 B 郑州

3 C 洛阳

4 D 新乡

declare stu1 student %rowtype;

add1 address %rowtype;

cursor student_cursor is

select * from student for update; --声明更新游标

cursor address_cursor is

select * from address; --声明游标

begin

open student_cursor; --打开游标

fetch student_cursor

into stu1; --移动游标

while student_cursor% found--判断游标是否指向某条记录

loop

open address_cursor; --打开另外一个游标

fetch address_cursor

into add1; --移动游标

while address_cursor %found--判断游标是否指向某条记录

loop

if add1.xh = stu1.xh then

--判断两个游标所指向的记录中xh的值是否相等

update student s

set s.zz = add1.zz

where current of student_cursor; --假如相等就更新游标所指向的记录值end if;

fetch address_cursor

into add1; --移动游标

end loop;

close address_cursor; --关闭游标

fetch student_cursor

into stu1; --移动游标

end loop;

close student_cursor; --关闭游标

end;

REF游标也叫动态游标:

qREF 游标和游标变量用于处理运行时动态执行的 SQL 查询 q创建游标变量需要两个步骤: q声明 REF 游标类型 q声明 REF 游标类型的变量 q用于声明REF 游标类型的语法为:

TYPE IS REF CURSOR

[RETURN ]; ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 -----------------------------------ref 游标

---------------------------------

declare

type ref_cursor is ref cursor; --声明一个ref 游标类型

tab_cursor ref_cursor ;--声明一个ref 游标

sname student.xm %type ;

sno student.xh %type ;

tab_name varchar2 (20 );

begin

tab_name := '&tab_name'; --接收客户输入的表明

if tab_name = 'student' then

open tab_cursor for select xh ,xm from student ; --打开ref 游标 fetch tab_cursor into sno ,sname ;--移动游标

while tab_cursor %found

loop

dbms_output.put_line ('学号:' ||sno ||'姓名:' ||sname ); fetch tab_cursor into sno ,sname ;

end loop;

close tab_cursor ;

else

dbms_output.put_line ('没有找到你想要找的表数据信息' ); end if ;

end;

-----------------------------------ref 游标题目

---------------------------------

SQL > select * from student ;

XH KC

---------- ----------

1 语文

1 数学

1 英语

1 历史

2 语文

2 数学

2 英语

3 语文

3 英语

9 rows selected

SQL >

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 完成的任务 :

生成student2表 (xh number, kc varchar2 (50 ));

对应于每一个学生,求出他的总的选课记录,把每个学生的选课记录插入到student2表中。

即,student2中的结果如下:

XH KC

--- ------------------------------------------- 1 语文数学英语历史

2 语文数学英语

3 语文英语

create table student2 (xh number, kc varchar2 (50 ));

declare

kcs varchar2 (50 );

kc varchar2 (50 );

type ref_cursor is ref cursor; --声明一个ref 游标类型

stu_cursor ref_cursor ;--定义一个ref 游标类型的变量

type tab_type is table of number; --声明一个table 类型

tab_xh tab_type ;--定义一个表类型的变量

cursor cursor_xh is select distinct( xh) from student; --声明一个游标 begin

open cursor_xh; --打开游标

fetch cursor_xh bulk collect into tab_xh; --提取数据到表中

for i in 1 .. tab_xh.count

loop

kcs :='' ;

open stu_cursor for select kc from student s where s.xh = tab_xh(i ); --打开ref 游标

fetch stu_cursor into kc ; --移动游标

while stu_cursor %found

loop

kcs := kc ||kcs ; --连接字符串使用||而不是+

fetch stu_cursor into kc ; --移动游标

end loop;

insert into student2 (xh , kc ) values( i, kcs);

close stu_cursor ;

end loop;

close cursor_xh ;

end;

pagesize 10

page 1

分页

between (page-1)*pagesize+1and page*pagesize

select * from(

select * from (select rownum rn,t.* from test1 t)) where rn between91and100;

游标与存储过程

实验5 游标与存储过程 1、实验目的 1. 学习实践游标与存储过程 2. 学习实践PL/SQL编程 2、实验原理 1. PL/SQL编程 2. 游标与存储过程 3、实验器材 1. 安装了Oracle,或者MySQL的计算机 4、实验内容 3. 创建表 Code Name Amt 01服装900 0101男装300 010101西装100 010102休闲装200 0102女装390 010201套装120 010202职业装130 010203休闲装140 0103童装210 02电器290 0201进口140 0202国产150 03日用品300 2.编写Oracle的存储过程,实现层次结构的逐级求和。

3.应用sql*plus,编写PL/SQL调用步骤2编写的存储过程。 五、实验报告要求 请将相应SQL语句写在实验报告上 1、 create table example(code number(10),name varchar2(20),amt number(10)); 2、 insert into example values(01,'服装',900); 3、 insert into example values(0101,'男装',300); 4、 insert into example values(010101,'西装',100); 5、 insert into example values(010102,'休闲装',200); 6、 insert into example values(0102,'女装',390); 7、 insert into example values(010201,'套装',120); 8、 insert into example values(010202,'职业装',130); 9、 insert into example values(010203,'休闲装',140); 10、 insert into example values(0103,'童装',210); 11、 insert into example values(02,'电器',290);、 12、 insert into example values(0201,'进口',140); select * from example; CODE NAME AMT --------- -------------------- ---------- 1 服装 900 101 男装 300 10101 西装 100 10102 休闲装 200 102 女装 390 10201 套装 120 10202 职业装 130 10203 休闲装 140

在Sql Server存储过程中使用Cursor(游标)操作记录

1.为何使用游标: 使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式。用SQL语言从数据库中检索数据后,结果放在内存的一块区域中,且结果往往是一个含有多个记录的集合。游标机制允许用户在SQL server内逐行地访问这些记录,按照用户自己的意愿来显示和处理这些记录。 2.如何使用游标: 一般地,使用游标都遵循下列的常规步骤: (1) 声明游标。把游标与T-SQL语句的结果集联系起来。 (2)打开游标。 (3)使用游标操作数据。 (4)关闭游标。 2.1.声明游标 DECLARE CURSOR语句SQL-92标准语法格式: DECLARE游标名[ INSENSITIVE ] [ SCROLL ] CURSOR FOR sql-statement Eg: Declare MycrsrVar Cursor FOR Select * FROM tbMyData 2.2打开游标 OPEN MycrsrVar 当游标被打开时,行指针将指向该游标集第1行之前,如果要读取游标集中的第1行数据,必须移动行指针使其指向第1行。就本例而言,可以使用下列操作读取第1行数据: FETCH FIRST from E1cursor 或FETCH NEXT from E1cursor 2.3 使用游标操作数据 下面的示例用@@FETCH_STATUS控制在一个WHILE循环中的游标活动 DECLARE E1cursor cursor FOR SELECT * FROM c_example OPEN E1cursor FETCH NEXT from E1cursor WHILE @@FETCH_STATUS = 0 BEGIN FETCH NEXT from E1cursor END CLOSE E1cursor DEALLOCATE E1cursor 2.4 关闭游标 使用CLOSE语句关闭游标 CLOSE { { [ GLOBAL ]游标名} |游标变量名} 使用DEALLOCATE语句删除游标,其语法格式如下: DEALLOCATE { { [ GLOBAL ]游标名} | @游标变量名

ex11游标与存储过程答案

实验十一游标与存储过程 (1)创建游标,逐行显示表Customer.的记录,并用WHILE结构来测试@@Fetch_Status的返回值。输出格式如下: '客户编号'+'-----'+'客户名称'+'----'+'客户地址'+'-----'+'客户电话 '+'------'+'客户邮编'+'------' DECLARE cur_cust SCROLL cursor FOR SELECT* FROM customer DECLARE @p_CustId char(5) DECLARE @p_CustName char(20) DECLARE @p_address char(40) DECLARE @p_Tel char(10) DECLARE @p_Zip char(6) DECLARE @p_All char(100) SELECT @p_All='客户编号'+'------'+'客户名称'+'------'+'客户地址 '+'-------------------------------------'+'客户电话'+'-------'+'客户邮 编'+'------' PRINT @p_All OPEN cur_cust FETCH cur_cust into @p_CustId,@p_CustName,@p_address,@p_Tel,@p_Zip WHILE(@@fetch_status<>-1) BEGIN SELECT @p_All=@p_CustId+' '+@p_CustName+@p_address+@p_Tel+' '+@p_Zip print @p_All FETCH cur_cust into @p_CustId,@p_CustName,@p_address,@p_Tel,@p_Zip END PRINT'客户数目: '+CONVERT(char(5),@@CURSOR_ROWS) CLOSE cur_cust DEALLOCATE cur_cust

实验16 游标、存储过程和函数参考答案

实验十六游标、存储过程和函数 一、目的与要求 1.了解游标的概念和工作原理; 2.了解存储过程的分类和使用方法; 3.了解触发器的概念; 4.学习编写和执行自定义过程; 5.学习编写和执行自定义函数; 6.学习创建和使用触发器。 二、实验准备 1.首先要了解游标是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了。将游标放置到某行后,即可对该行数据进行操作,最常见的操作是提取当前行数据。 2.使用显式游标的步骤: (1)说明游标。 (2)打开游标。 (3)读取数据。 (4)关闭游标。 3.了解PL/SQL包括3种存储过程,即过程、函数和程序包。 4.了解触发器是一种特殊的存储过程,当指定表中的数据发生变化时自动运行。 三、实验内容 1.练习书上的例子10.1—10.24。 2.以上机实验经常用到的数据库LIB为例,编写过程P_ResetPrice,此过程的功能是将表图书中指定书号的单价更改为10.0,调用该过程将书号为’TP311.13/CM3’的单价更改为10.0,将程序写在实验报告中。 create or replace procedure P_ResetPrice (vBno in varchar2) as begin update 图书 set 单价=10.0 where 图书号=vBno; end; execute P_ResetPrice(‘TP311.13/CM3’); 3.编写一函数F_GetBName,该函数的功能是在图书中根据指定的书号,返回该书的书名,并在匿名块中调用函数F_GetBName找出编号为“TP311.132/ZG1”的书名,将程序写在实验报告中。 create function F_GetName (vtno IN 图书.图书号%Type) return 图书.书名%Type

oracle游标的使用及属性

oracle游标的使用及属性 oracle游标的使用 游标是从数据表中提取出来的数据,以临时表的形式存放到内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中的数据进行各种操作,然后将操作结果写回到数据库中。 一:定义游标 cursor游标名isselect语句; 示例: setserveroutputon declare tempsalscott.emp.sal%type; cursormycursorisselect *fromscott.empwheresal>tempsal; begin tempsal:=800; openmycursor; end; 二:打开游标 语法结构:open游标名 打开游标分为两步:1将符合条件的记录送入内存2将指针指向第一条记录 三:提取游标数据

语法形式:fetch游标名into变量名1,变量名2,.....;或者 fetch游标名into记录型变量名; 示例: setserveroutputon declare tempsalscott.emp.sal%type; cursormycursorisselect*fromscott.empwheresal>tempsal; cursorrecordmycursor%rowtype; begin tempsal:=800; openmycursor; fetchmycursorintocursorrecord; dbms_output.put_line(to_char(cursorrecord.deptno)); end; 四:关闭游标 close游标名; Oracle游标的属性之一------%isopen %isopen 属性----测试游标是否打开,没打开的情况下使用fetch语句将提示错误。 示例:

实验九 游标与存储过程

实验九游标与存储过程 1 实验目的与要求 (1) 掌握游标的定义和使用方法。 (2) 掌握存储过程的定义、执行和调用方法。 (3) 掌握游标和存储过程的综合应用方法。 2 实验内容 请完成以下实验内容: (1)创建游标,逐行显示Customer表的记录,并用WHILE结构来测试 @@Fetch_Status的返回值。输出格式如下: declare @C_no char(9),@C_name char(18),@C_phone char(10), @C_addchar(8),@C_zip char(6) declare @text char(100) declarecus_cur scroll cursor for select* from Customer select @text='=========================Customer 表的记录 =========================' print @text select @text='客户编号'+'-----'+'客户名称'+'----'+'客户住址'+'-----'+'客户电话'+'------'+'邮政编码' print @text select @text='============================================================ ============================' print @text opencus_cur fetchcus_cur into @C_no,@C_name,@C_phone,@C_add,@C_zip while(@@fetch_status=0) begin select @text=@cust_No+' '+@cust_name+' '+@addr+' '+@tel_no+' '+@zip print @text fetchcus_cur into @C_no,@C_name,@C_phone,@C_add,@C_zip end closecus_cur deallocatecus_cur '客户编号'+'-----'+'客户名称'+'----'+'客户住址'+'-----'+'客户电话'+'------'+'邮政编码'

Oracle 游标使用全解

-- 声明游标;CURSOR cursor_name IS select_statement --For 循环游标 --(1)定义游标 --(2)定义游标变量 --(3)使用for循环来使用这个游标 declare --类型定义 cursor c_job is select empno,ename,job,sal from emp where job='MANAGER'; --定义一个游标变量v_cinfo c_emp%ROWTYPE ,该类型为游标c_emp中的一行数据类型 c_row c_job%rowtype; begin for c_row in c_job loop dbms_output.put_line(c_row.empno||'-'||c_row.ename||'-'||c_row.job||'-'||c_row.sal); end loop; end; --Fetch游标 --使用的时候必须要明确的打开和关闭 declare --类型定义 cursor c_job is select empno,ename,job,sal from emp where job='MANAGER'; --定义一个游标变量 c_row c_job%rowtype; begin open c_job; loop --提取一行数据到c_row fetch c_job into c_row; --判读是否提取到值,没取到值就退出 --取到值c_job%notfound 是false --取不到值c_job%notfound 是true exit when c_job%notfound; dbms_output.put_line(c_row.empno||'-'||c_row.ename||'-'||c_row.job||'-'||c_row.sal);

SQL游标嵌套存储过程

--测试数据 create table tmp1 ( ID int not null, val varchar(10), constraint PK_tmp1 primary key (ID) ); create table tmp2 ( ID int not null, vals varchar(100), constraint PK_tmp2 primary key (ID) ); insert into tmp1(id, val) values (1, 'test'); insert into tmp1(id, val) values (2, 'test2'); insert into tmp1(id, val) values (3, 'test3'); insert into tmp1(id, val) values (4, 'test4'); insert into tmp1(id, val) values (5, 'test5'); insert into tmp2(id, vals) values (1, '1,2'); insert into tmp2(id, vals) values (2, '1,3'); insert into tmp2(id, vals) values (3, '2,5'); insert into tmp2(id, vals) values (4, '1,2,3,4,5'); --存储过程 drop procedure proc_tmp_1 go CREATE PROCEDURE proc_tmp_1 AS begin declare @vals varchar(500) declare @id int declare @vals2 varchar(1000) declare @command varchar(1000) declare @vals3 varchar(1000) declare @cmd varchar(1000) declare cursor_tmp_1 cursor for SELECT id, vals FROM tmp2 open cursor_tmp_1 fetch next from cursor_tmp_1 into @id, @vals while @@fetch_status = 0 begin set @vals3 = '' set@cmd = 'declare cursor_tmp_2 cursor for select val from tmp1 where id in (' + @vals + ')' EXEC (@cmd) open cursor_tmp_2 fetch next from cursor_tmp_2 into @vals2 while @@fetch_status = 0 begin if (@vals3 <> '') begin set @vals3 = @vals3 + ',' end SET @vals3 = @vals3 + @vals2

Oracle显式游标和隐式游标

Oracle显式游标和隐式游标 SQL是用于访问Oracle数据库的语言,PL/SQL扩展和加强了SQL的功能,它同时引入了更强的程序逻辑, 下面在本文中将对游标的使用进行一下讲解,希望可以和大家共同学习进步。 游标字面理解就是游动的光标。游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或最终写回数据库。这样数据处理的速度才会提高,否则频繁的磁盘数据交换会降低效率。用数据库语言来描述游标就是映射在结果集中一行数据上的位置实体,有了游标,用户就可以访问结果集中的任意一行数据了,将游标放置到某行后,即可对该行数据进行操作,例如提取当前行的数据等。 游标有两种类型:显式游标和隐式游标。游标一旦打开,数据就从数据库中传送到游标变量中,然后应用程序再从游标变量中分解出需要的数据,并进行处理。 当系统使用一个隐式游标时,可以通过隐式游标的属性来了解操作的状态和结果,进而控制程序的流程。隐式游标可以使用名字SQL来访问,但要注意,通过SQL游标名总是只能访问前一个处理操作或单行SELECT操作的游标属性。所以通常在刚刚执行完操作之后,立即使用SQL游标名来访问属性。游标的属性有四种,分别是 SQL %ISOPEN,SQL %FOUND,SQL %NOTFOUND,SQL %ROWCOUNT。 SQL%ISOPEN返回的类型为布尔型,判断游标是否被打开,如果打开%ISOPEN等于true,否则等于false,即执行过程中为真,结束后为假。 SQL%NOTFOUND返回值为布尔型,判断游标所在的行是否有效,如果有效, 则%FOUNDD等于true,否则等于false,即与%FOUND属性返回值相反。 SQL%FOUND返回值的类型为布尔型,值为TRUE代表插入删除更新或单行查询操作成功。 SQL%ROWCOUNT返回值类型为整型,返回当前位置为止游标读取的记录行数,

sql server存储过程详细代码(超赞!)

use jxgl --首先判断有没有已经建立up_getallstudents存储过程,有则先删除 if exists (select name from sysobjects where name='up_getallstudents'and type ='p') drop procedure up_getallstudents --编写存储过程up_getallstudents,用于获取学生表students的所有记录 create procedure up_getallstudents as select*from students --使用execute执行存储过程up_getallstudents exec up_getallstudents --也可写成 execute up_getallstudents --编写一个存储过程up_insertstudent,完成学生表students数据的插入 --1、不带默认值的参数 create procedure up_insertstudent @sid varchar(15),@sname varchar(30),@ssex char(10), @sbirth datetime,@sbirthplace varchar(300) as begin insert into students (stu_id,stu_name,stu_sex,stu_birth,stu_birthplace) values (@sid,@sname,@ssex,@sbirth,@sbirthplace) end exec up_insertstudent'200712110111','肖玉峰','男','1975-02-05','山东省滕州市木石镇' --等同于 exec up_insertstudent @sname='肖玉峰',@sid='200712110111',@ssex='男',@sbirth= '1975-02-05',@sbirthplace='山东省滕州市木石镇' drop procedure up_insertstudent delete students where stu_name='肖玉峰'

oracle11g游标及触发器相关知识

oracle11g 游标: 1. 当在PL/SQL中使用SQL语句时,Oracle会为其分配上下文区域,这是一段 私有的内存区域,用于暂时保存SQL语句影响到的数据。游标是指向这段内存区域的指针。 2. Oracle中主要有两种类型的游标: (1) 隐式游标:所有的DML语句和PL/SQL SELECT 语句都有; (2) 显式游标:由开发人员声明和控制。 3. 可以使用的游标属性包括四种:%ROWCOUNT、%FOUND、%NOTFOUND、 %ISOPEN,这四种属性对于显式游标和隐式游标都有用,但是含义和使用方法略有不同。游标在使用属性时,需要以游标名称作为前缀,以表明该属性是哪个游标的,隐式游标没有名称,所以在使用隐式游标时采取了统一的一个名称SQL。 4. 在PL/SQL中的SELECT语句只能且必须取出一行数据,取出多行或者零行都 被认为是异常,所以在对多行数据进行操作时,必须使用显式游标来实现。 5. 使用显式游标的步骤: (1)声明游标:CURSOR cursor_name is select_statement; (2)打开游标:OPEN cursor_name; (3)取游标中的数据:FETCH cursor_name INTO variable1,variable2,...; (4)关闭游标:CLOSE cursor_name; 6.用变量接收游标中的数据 sql> declare v_name emp.ename%TYPE; v_sal emp.sal%TYPE; cursor emp_cursor is select ename,sal from emp where deptno=10; begin open emp_cursor; loop fetch emp_cursor into v_name,v_sal; exit when emp_cursor%NOTFOUND; dbms_output.put_line(v_name || ‘的薪水是’ || v_sal);

Oracle 函数返回游标的方法及应用

Oracle函数返回游标的方法及应用简析 王凤利康俊霞 河北北方学院张家口职业技术学院 【摘要】Oracle函数除了可以返回数值类型和字符类型等常用数据类型的数 据以外,还可以返回游标类型的数据,在某些情况下,返回游标类型的函数为我们 解决一些应用中的难题。本文就是通过一个实例来介绍返回游标函数的创建过程。 【关键字】Oracle,函数,包,游标。 1 问题的提出 在油田信息系统建设过程中,遇到了这样一个问题:输油站泵运行数据表(DHC05)的结构为:时间(SJ)、单位名称(DWMC)、泵号(BH)、泵压(BY1)、电压(DY)、电流(DL)、排量(PL),主键为:时间、单位名称、泵号,每整点对运行泵取一次数据,不存储不运行泵(运行时间为0)的数据。现要输出如下报表: 报表的查询条件为单位名称和日期(yyyymmdd格式字符串)。运行泵号及泵台数根据各单位实际运行情况而定。 2 系统简介 系统采用B/S三层体系结构,数据库采用Oracle9.2版本,WEB服务器采用IIS6.0版本,客户端采用IE6.0及以上版本。 报表采用了统一的制表解释程序进行输出,该解释程序可以从一个单一的Oracle查询语句中提取数据,报表的输出样式为简单的二维表。 3 解决方案 根据目前的系统现状,要想直接利用当前系统输出本报表是不可能的,经过分析认为共有以下几种解决方案: a)修改数据表结构 将原始数据表的结构进行调整,把时间、单位名称作为主键,将同一时刻的各个泵的数据逐个列出作为数据列,大致结构为:时间、单位名称、1号泵泵压、1号泵电压、1号泵电流、1号泵排量、2号泵泵压、2号泵电压、2号泵电流、2号泵排量、…。 本方案违背了数据库设计的基本原则,因各个单位的泵数不相等,只能按最大泵数进行数据库结构设计,当站库继续扩大、泵数继续增加时需要对数据库结构和所有用到该数据表

存储过程和游标

我们在进行pl/sql编程时打交道最多的就是存储过程了。存储过程的结构是非常的简单的,我们在这里除了学习存储过程的基本结构外,还会学习编写存储过程时相关的一些实用的知识。如:游标的处理,异常的处理,集合的选择等等 1.存储过程结构 1.1 第一个存储过程 Java代码 1.create or replace procedure proc1( 2. p_para1 varchar2, 3. p_para2 out varchar2, 4. p_para3 in out varchar2 5.)as 6. v_name varchar2(20); 7.begin 8. v_name := '三丰'; 9. p_para3 := v_name; 10. dbms_output.put_line('p_para3:'||p_para3); 11.end; 上面就是一个最简单的存储过程。一个存储过程大体分为这么几个部分: 创建语句:create or replace procedure 存储过程名 如果没有or replace语句,则仅仅是新建一个存储过程。如果系统存在该存储过程,则会报错。Create or replace procedure 如果系统中没有此存储过程就新建一个,如果系统中有此存储过程则把原来删除掉,重新创建一个存储过程。 存储过程名定义:包括存储过程名和参数列表。参数名和参数类型。参数名不能重复,参数传递方式:IN, OUT, IN OUT IN 表示输入参数,按值传递方式。 OUT 表示输出参数,可以理解为按引用传递方式。可以作为存储过程的输出结果,供外部调用者使用。 IN OUT 即可作输入参数,也可作输出参数。 参数的数据类型只需要指明类型名即可,不需要指定宽度。 参数的宽度由外部调用者决定。 过程可以有参数,也可以没有参数 变量声明块:紧跟着的as (is )关键字,可以理解为pl/sql的declare关键字,用于声明变量。 变量声明块用于声明该存储过程需要用到的变量,它的作用域为该存储过程。另外这里声明的变量必须指定宽度。遵循PL/SQL的变量声明规。 过程语句块:从begin 关键字开始为过程的语句块。存储过程的具体逻辑在这里来实现。 异常处理块:关键字为exception ,为处理语句产生的异常。该部分为可选 结束块:由end关键字结果。 1.2 存储过程的参数传递方式 存储过程的参数传递有三种方式:IN,OUT,IN OUT . IN 按值传递,并且它不允许在存储过程中被重新赋值。如果存储过程的参数没有指定存参数传递类型,默认为IN

Oracle存储过程学习_游标CURSOR使用

游标CURSOR的使用学习 游标的类型: 1,隐式游标:在 PL/SQL 程序中执行DML SQL 语句时自动创建隐 式游标,名字固定叫sql。 2,显式游标:显式游标用于处理返回多行的查询。 3,REF 游标:REF 游标用于处理运行时才能确定的动态 SQL 查询 的结果 一、隐式游标 在PL/SQL中使用DML语句时自动创建隐式游标 q隐式游标自动声明、打开和关闭,其名为 SQL q通过检查隐式游标的属性可以获得最近执行的DML 语句的信息q隐式游标的属性有: q%FOUND – SQL 语句影响了一行或多行时为 TRUE q%NOTFOUND – SQL 语句没有影响任何行时为TRUE q%ROWCOUNT – SQL 语句影响的行数 q%ISOPEN - 游标是否打开,始终为FALSE begin update student s set s.sage = s.sage + 10; if sql %FOUND then dbms_output.put_line('这次更新了' || sql% rowcount); else dbms_output.put_line('一行也没有更新'); end if; end; 在select中有两个中比较常见的异常: 1. NO_DATA_FOUND 2. TOO_MANY_ROWS declare sname1 student.sname%TYPE; begin

select sname into sname1 from student; if sql%found then dbms_output.put_line(sql%rowcount); else dbms_output.put_line('没有找到数据'); end if; exception when too_many_rows then dbms_output.put_line('查找的行记录多于1行'); when no_data_found then dbms_output.put_line('未找到匹配的行'); end; 显式游标: sqlserver与oracle的不同之处在于:最后sqlserver会deallocate 丢弃游标,而oracle只有前面四步:声明游标、打开游标、使用游标读取记录、关闭

【黑马程序员】Oracle 游标使用全解

【黑马程序员】Oracle 游标使用全解 有很多同学在使用oracle 数据库的时候对游标这个东西不知道如何处理,感觉使用起来很难,今天我们就讨论一下游标的使用,满满的都是干货,以下代码几乎包含了oracle 游标使用的方方面面,全部通过了测试! -- 声明游标; 1 C URSOR cursor_name IS select_statement --For 循环游标 --(1)定义游标 --(2)定义游标变量 --(3)使用for 循环来使用这个游标 01 02 03 04 05 06 07 08 09 10 11 12 13 14 declare --类型定义 cursor c_job is select empno ,ename ,job ,sal from emp where job ='MANAGER'; --定义一个游标变量v_cinfo c_emp%ROWTYPE ,该类型为游标c_emp 中的一行数据类型 c_row c_job%rowtype; begin for c_row in c_job loop dbms_output.put_line (c_row.empno||'-'||c_row.ename||'-'||c_row.job||'-'||c_row.sal ); end loop; end ; --Fetch 游标 --使用的时候必须要明确的打开和关闭 01 02 03 declare --类型定义 cursor c_job

04 05 06 07 08 09 10 11 12 13 14 15 16 is select empno,ename,job,sal from emp where job='MANAGER'; --定义一个游标变量 c_row c_job%rowtype; begin open c_job; loop --提取一行数据到c_row fetch c_job into c_row; --判读是否提取到值,没取到值就退出 --取到值c_job%notfound 是false --取不到值c_job%notfound 是true exit when c_job%notfound; dbms_output.put_line(c_row.empno||'-'||c_row.ename||'-'||c_row.job||' -'||c_row.sal); end loop; --关闭游标 close c_job; end; --1:任意执行一个update操作,用隐式游标sql的属 性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。 begin update emp set ENAME='ALEARK' WHERE EMPNO=7469; if sql%isopen then dbms_output.put_line('Openging'); else dbms_output.put_line('closing'); end if; if sql%found then dbms_output.put_line('游标指向了有效行');--判断游标是否指向有效行 else dbms_output.put_line('Sorry'); end if; if sql%notfound then dbms_output.put_line('Also Sorry'); else dbms_output.put_line('Haha'); end if;

存储过程与游标练习

创建三个表:学生(学号,姓名)、课程(课程号,课程名)、成绩(学号、课程号、分数),然后在三个表中分别添加记录。按照输入的课程名称打印此门课程的成绩报表(如不给定课程名称则打印SQL课程的成绩),输出结果按照分数降序排列: 例如: 《SQL》成绩表 **************************************************** 名次学号姓名成绩 1 0508044126 李军95 2 0508044124 李明85 3 0508044125 王刚75 **************************************************** */ use pubs IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '学生') DROP table 学生 GO IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '课程') DROP table 课程 GO IF EXISTS (SELECT NAME FROM sysobjects WHERE NAME = '成绩') DROP table 成绩 GO create table 学生(学号char(10) primary key constraint xh_chk check (学号like '0508044[1-4][0-3][0-9]'),姓名nvarchar(10) not null) create table 课程(课程号char(6) primary key,课程名称nvarchar(40)) create table 成绩(学号char(10) not null,课程号char(6) not null,分数numeric(4,1)) insert 学生values('0508044124','李明') insert 学生values('0508044125','王刚') insert 学生values('0508044126','李军') insert 课程values('080101','SQL') insert 课程values('080204','D S') insert 成绩values('0508044124','080101',85) insert 成绩values('0508044124','080204',95) insert 成绩values('0508044125','080101',75) insert 成绩values('0508044125','080204',86) insert 成绩values('0508044126','080101',95) go if exists(select * from sysobjects where name='cj_proc' and xtype='p') drop proc cj_proc

Oracle数据库游标在包中的使用

--创建学员信息表 create table stuInfo ( stuId varchar2(15) not null, --学员Id,主键 stuName varchar2(10) not null, --学员姓名 stuNo varchar2(10) not null, --学号,外键应用stuMarks的stuNo stuAge int not null, --年龄 stuAddress varchar2(100) default('中国') not null,--家庭住址 stuEmail varchar2(100) not null --电子邮箱 ); alter table stuInfo add constraint PK_stuId primary key(stuId); alter table stuInfo add constraint CK_stuAge check(stuAge between 18 and 40); alter table stuInfo add constraint CK_stuEmail check(stuEmail like '%@%'); --创建序列 create sequence SQ_ID increment by 1 start with 10000; --为学员信息表创建触发器TG_STUID create or replace trigger TG_STUID before insert on stuInfo for each row begin select 'SID'||SQ_ID.Nextval into :new.stuId from dual; end; --向学员信息表中添加数据 insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('张飞','s1t0102',30,'三国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('关羽','s1t0830',35,'蜀国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('马超','s2t1326',25,'三国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('刘备','s3t0403',40,'蜀国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('诸葛亮','s2t1521',21,'蜀国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('刘翔','s3t0706',29,'上海',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('曹操','s3t0915',34,'魏国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('孙权','s1t1123',32,'东吴',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values ('董卓','s2t0507',35,'三国',''); insert into stuInfo(stuName,stuNo,stuAge,stuAddress,stuEmail) values

Oracle10.2并发条件下更新游标数据的研究

Oracle10.2并发条件下更新游标数据的研究 本文测试在PL/SQL编程中,更新游标数据的2种方式以及并发条件下各种方式的实际表现。2种方式的效率问题不在此文讨论之列! 一、环境准备 数据库:Oracle10.2.0.4 测试工具:PL/SQL Developer9 二、数据准备 我们使用oracle自带的演示用户scott登录数据库,为清楚看到数据变化,执行以下语句: 查询emp表: 结果如图1所示:

图1 Sal字段已经全部更新为1000 三、创建存储过程ps_cursor_for_update

四、测试ps_cursor_for_update PL/SQL Developer工具具有很强大的plsql调试功能,我们使用两个test窗口进行模拟并发执行的情况 在编辑存储过程的界面,在打开游标的代码行加入一个断点: 图2 在存储过程ps_cursor_for_update上点击右键,打开两个test窗口: 窗口1中,输入参数填写2000 窗口2中,输入参数填写3000,如图3、4所示: 图3 图4 两个窗口分别点击start bugger按钮,开始调试,并点击run按钮,分别运行到打开游标的一行,并在窗口2中进行单步调试,运行到如图5所示位置:

图5 此时窗口1中,开始单步调试,发现状态栏处于运行中(图7),但调试光标始终停在断点行(图6) 图6 图7 说明游标打开的数据已经被窗口2的进程锁定,所以窗口1的进程无法打开数据 下面把窗口2的断点去掉,并点击run按钮使此过程执行完毕,可以发现,此时窗口1中,代码已经执行到了原断点位置的下一行: 图8 结论:窗口2执行了commit语句,PL/SQL过程结束,并解锁操作的数据,使窗口1的过程得以打开游标。 查询emp表的数据:

相关文档
最新文档