
游标就是一个指针,查询一个结果集,这个指针可以指向结果集中任何一条记录。可以理解为指向结果集记录的指针,但是只能返回一行记录,如果需要多行就需要使用LOOP语句。
数据库有三种模式:模式、外模式、内模式。
通俗来讲模式就是开发人员通过需求设计一个逻辑结构。外模式就是用户面对的某一部分。内模式就是存储介质上存储方式的物理结构。
游标是SQL的一个内存工作区 由系统或用户以变量的形式定义 游标的作用就是用于临时存储从数据库中提取的数据块
Oracle数据库的Cursor类型包含三种 静态游标 分为显式(explicit)游标和隐式(implicit)游标 REF游标 是一种引用类型 类似于指针
测试数据
create table student(sno number primary key sname varchar ( ))
declare i number:= ;
beginwhile i<=
loop
insert into student(sno sname) values (i name ||to_char(i))
i:=i+ ;
end loop;
end;
隐式游标属性
SQL%ROWCOUNT 整型代表DML语句成功执行的数据行数
SQL%FOUND 布尔型值为TRUE代表插入 删除 更新或单行查询 *** 作成功
SQL%NOTFOUND 布尔型与SQL%FOUND属性返回值相反
SQL%ISOPEN 布尔型DML执行过程中为真 结束后为假
declarebegin update student set sname = name ||to_char(sno ) where sname= name ;
if sql%found then
dbms_output put_line( name is updated )
else
dbms_output put_line( 没有记录 )
end if;
end;
declare
begin
for names in (select from student) loop
dbms_output put_line(names sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
显式游标属性
%ROWCOUNT 获得FETCH语句返回的数据行数
%FOUND 最近的FETCH语句返回一行数据则为真 否则为假
%NOTFOUND 布尔型 与%FOUND属性返回值相反
%ISOPEN 布尔型 游标已经打开时值为真 否则为假
对于显式游标的运用分为四个步骤
a 定义游标 Cursor [Cursor Name] IS;
b 打开游标 Open [Cursor Name];
c *** 作数据 Fetch [Cursor name];
d 关闭游标 Close [Cursor Name];
典型显式游标
declare cursor cur_rs is select from student; sinfo student%rowtype;
begin open cur_rs;
loop
fetch cur_rs into sinfo;
exit when cur_rs%%notfound;
dbms_output put_line(sinfo sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
带参数open的显式cursor:
declare cursor cur_rs(in_name varchar ) is select
from student where sname=in_name;
begin for sinfo in cur_rs( sname ) loop
dbms_output put_line(sinfo sname)
end loop;
exception when others then
dbms_output put_line(sqlerrm)
end;
使用current of语句执行update或delete *** 作
declare
cursor cur_rs is select from student for update;
begin for sinfo in cur_rs loop
update student set sname=sname|| xx where current of cur_rs;
end loop;
mit;
exception when others then
dbms_output put_line(sqlerrm)
end;
REF游标 用于处理运行时才能确定的动态sql查询结果 利用REF CURSOR 可以在程序间传递结果集(一个程序里打开游标变量 在另外的程序里处理数据)
也可以利用REF CURSOR实现BULK SQL 提高SQL性能
REF CURSOR分两种 Strong REF CURSOR 和 Weak REF CURSOR
Strong REF CURSOR: 指定retrun type CURSOR变量的类型必须和return type一致
Weak REF CURSOR: 不指定return type 能和任何类型的CURSOR变量匹配
运行时根据动态sql查询结果遍历
create or replace package pkg_test as
type student_refcursor_type is ref cursor return student%rowtype;
procedure student_rs_loop(cur_rs IN student_refcursor_type)
end pkg_test ;
create or replace package body pkg_test as
procedure student_rs_loop(cur_rs IN student_refcursor_type) is
std student%rowtype;
begin loop
fetch cur_rs into std;
exit when cur_rs%NOTFOUND;
dbms_output put_line(std sname)
end loop;
end student_rs_loop;
end pkg_test ;
declare stdRefCur pkg_test student_refcursor_type;
begin for i in loop
dbms_output put_line( Student NO= || i)
open stdRefCur for select from student where sno=i;
pkg_test student_rs_loop(stdRefCur)
end loop;
exception when others then dbms_output put_line(sqlerrm)
close stdRefCur;
end;
使用FORALL和BULK COLLECT子句 利用BULK SQL可以减少PLSQL Engine和SQL Engine之间的通信开销 提高性能
加速INSERT UPDATE DELETE语句的执行 也就是用FORALL语句来替代循环语句
加速SELECT 用BULK COLLECT INTO 来替代INTO
create table
student_tmp as select sno
sname from student where = ;
删除主键约束 alter table student drop constraint SYS_C ;
执行两遍插入 insert into student select from student where sno= ;
declare cursor cur_std(stdid student sno%type) is select sno
sname from student where sno=stdid;
type student_table_type is table of cur_std%rowtype index by pls_integer;
student_table student_table_type;
begin
open cur_std( )
fetch cur_std bulk collect into student_table;
close cur_std;
for i in unt loop
dbms_output put_line(student_table(i) sno ||
|| student_table(i) sname)
end loop;
forall i in student_table firststudent_table last
insert into student_tmp values(student_table(i) sno student_table(i) sname)
mit;
end;
lishixinzhi/Article/program/Oracle/201311/17358
简单来说,游标就是一个临时存储器。SQL处理数据时,总是对整个数据集打包给你,而不会逐条给你,而游标就是将SQL打包的数据集进行二次处理,使得你能够逐条对数据修改和处理。就是将机器的死脑经变的更人性化一点,满足人们的日常需要(哈哈哈,皮一下!)
mysql的存储过程定义一个游标
-- 定义游标cursor c_emp is select ename,job from emp where xx;-
当作普通的查询语句就OK啦!
windows或DOS的“光标”不同,MS-SQL的游标是一种临时的数据库对象,既对可用来旋转储存在系统永久表中的数据行的副本,也可以指向储存在系统永久表中的数据行的指针。
游标为您提供了在逐行的基础上而不是一次处理整个结果集为基础的 *** 作表中数据的方法。 1.如何使用游标 1)定义游标语句 Declare <游标名> Cursor For
2)创建游标语句 Open <游标名>
3)提取游标列值、移动记录指针 Fetch <列名列表> From <游标名> [Into <变量列表>]
4)使用@@Fetch_Status利用While循环处理游标中的行
5)删除游标并释放语句 Close <游标名>/Deallocate <游标名>
6)游标应用实例 --定义游标
Declare cur_Depart Cursor
For Select cDeptID,cDeptName From Department into @DeptID,@DeptName
--创建游标
Open cur_Depart
--移动或提取列值
Fetch From cur_Depart into @DeptID,@DeptName
--利用循环处理游标中的列值
While @@Fetch_Status=0
Begin
Print @DeptID,@DeptName
Fetch From cur_Depart into @DeptID,@DeptName
End
--关闭/释放游标
Close cur_Depart
Deallocate cur_Depart2.语句的详细及注意 1)定义游标语句 Declare <游标名> [Insensitive] [Scroll] Cursor
For <Select 语句> [FOR {Read Only | Update [ OF <列名列表>]}] Insensitive DBMS创建查询结果集数据的临时副本(而不是使用直接引用数据库表中的真实数据行中的列)。游标是Read Only,也就是说不能修改其内容或底层表的内容; Scroll 指定游标支持通过使用任意Fetch 选项(First Last Prior Next Relative Absolute)选取它的任意行作为当前行。如果此项省略,则游标将只支持向下移动单行(即只支持游标的Fetch Next); Select语句 定义游标结果集的标准 SELECT 语句。在游标声明的 <Select语句>内不允许使用关键字 COMPUTE、COMPUTE BY、FOR BROWSE 和 INTO; Read Only 防止使用游标的用户通过更新数据或删除行改变游标的内容; Update 创建可更新游标且列出值能被更新的游标列。如果子句中列入了任意列,则只有被列入的列才能被更新。如果Declare Cursor语句中只指定的UPDATE(没有列名列表),则游标将允许更新它的任何或所有列。
Declare cur_Depart Cursor
For Select From Department For Update OF cDeptID,cDeptName 2)提取游标列值、移动记录指针语句 Fetch [Next | Prior | First | Last | {Absolute <行号>} | {Relative <行号>}]
From <游标名> [Into <变量列表……>] 每次执行Fetch语句时,DBMS移到游标中的下一行并把游标中的列值获取到Into中列出的变量中。因此Fetch语句的Into子句中列出的变量必须与游标定义中Select 语句中的列表的类型与个数相对应; 仅当定义游标时使用Scroll参数时,才能使用Fetch语句的行定位参数(First Last Prior Next Relative Absolute);如果Fetch语句中不包括参数Next | Prior | First | Last,DBMS将执行默认的Fetch Next; Next 向下、向后移动一行(记录); Prior 向上、向前移动一行(记录); First 移动至结果集的第一行(记录); Last 移动至结果集的最后一行(记录); Absolute n 移动到结果集中的第n行。如果n是正值,DBMS从结果集的首部向后或向下移动至第n行;如果n是负数,则DBMS从结果集的底部向前或向上移动n行;
Fetch Absolute 2 From cur_Depart Into @DeptID,@DeptName Relative n 从指针的当前位置移动n行。如果n是正值,DBMS将行指针向后或向下移动至第n行;如果n是负数,则DBMS将行指针向前或向上移动n行;
Fetch Relative 2 From cur_Depart Into @DeptID,@DeptName 3)基于游标的定位DELETE/UPDATE语句 如果游标是可更新的(也就是说,在定义游标语句中不包括Read Only 参数),就可以用游标从游标数据的源表中DELETE/UPDATE行,即DELETE/UPDATE基于游标指针的当前位置的 *** 作;
举例: --删除当前行的记录
Declare cur_Depart Cursor
For Select cDeptID,cDeptName From Department into @DeptID,@DeptName
Open cur_Depart
Fetch From cur_Depart into @DeptID,@DeptName
Delete From Department Where CURRENT OF cur_Depart --更新当前行的内容
Declare cur_Depart Cursor
For Select cDeptID,cDeptName From Department into @DeptID,@DeptName
Open cur_Depart
Fetch From cur_Depart into @DeptID,@DeptName
Update Department Set cDeptID=’2007’ + @DeptID Where CURRENT OF cur_Depart3.游标使用技巧及注意 1)利用Order By改变游标中行的顺序。此处应该注意的是,只有在查询的中Select 子句中出现的列才能作为Order by子句列,这一点与普通的Select语句不同;
2)当语句中使用了Order By子句后,将不能用游标来执行定位DELETE/UPDATE语句;如何解决这个问题,首先在原表上创建索引,在创建游标时指定使用此索引来实现;例如:
Declare cur_Depart Cursor
For Select cDeptID,cDeptName From Department With INDEX(idx_ID)
For Update Of cDeptID,cDeptName
通过在From子句中增加With Index来实现利用索引对表的排序;
3)在游标中可以包含计算好的值作为列;
4)利用@@Cursor_Rows确定游标中的行数;
以上就是关于数据库里面的游标,内模式,模式,外模式什么意思全部的内容,包括:数据库里面的游标,内模式,模式,外模式什么意思、Oracle数据库游标的类型、SQL 数据库中的游标指的是什么,有什么作用等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)