第11章 游标
行级触发器里的:
系统事件触发器:
create or replace trigger 触发器名
{before|after}触发事件1 or 触发事件2 or 触发事件3
on 表名
declare
声明部分
begin
执行部分
end;
说明:
数据库事件:startup,shutdown,servererror,db_role_change
由系统管理员SYS创建
eg:
–系统事件触发器
–功能:记录数据库实例启动的时间
–以sys用户登录
create table eventLog (
event varchar2(30),
eventTime date
);
–只能用after
create or replace trigger tir_startup after startup on database
begin
insert into eventLog values(ora_sysevent,sysdate);
end;
–测试触发器
–关闭实例
SQL->shutdown immediate;
–启动实例
SQL->startup
–查看审计表eventLog
SQL->select *from eventLog;
游标:
一、ORACLE游标的分类:
1、显示游标:处理select语句返回的多行记录
2、隐示游标:执行DML语句或处理单行select…into 语句
3、动态游标(ref):处理运行时才能确定的动态SQL查询的结果;
二、隐示游标
游标名:SQL
游标的属性:%isopen,%rowcount,%found,%notfound
下面是游标的引用方式:
SQL%isopen-- 是否打开
SQL%rowcount–表示受影响的行数
SQL%found-- 表示几行数据受影响
SQL%notfound–没有数据受到影响
eg:
–1、执行单行查询,通过隐示游标的属性查看执行的结果
declare
vSname Student.Sname%type;
vSdept Student.Sdept%type;
begin
select Sname,Sdept into vSname ,vSdept from Student where Sno=‘2017002’;
if SQL%isopen then
dbms_output.put_line(‘隐示游标是打开的’);
else
dbms_output.put_line(‘隐示游标是关闭的’);
end if;
if SQL%found then
dbms_output.put_line(SQL%rowcount||‘行受影响’);
dbms_output.put_line(‘姓名:’||vSname||’,系:’||vSdept);
end if;
end;
–这个属性会显示几行受影响,但是无太多实际意义
-----------SQLServer--------------------
select*from Student
select @@rowcount
select *from Student where Sno=‘2017001’
2、执行DML语句
eg:–执行DML语句
begin
update SC set Grade=60 where Sno=‘2017002’;
if SQL%notfound then
dbms_output.put_line(‘SC表中没有数据被更新’);
else
dbms_output.put_line(‘SC表中有’||SQL%rowcount||‘行被更新’);
insert into SC values(‘2017004’,‘1’,100);
if SQL%found then
dbms_output.put_line(‘向SC表中插入了’||SQL%rowcount||‘条记录’);
endif;
end;
三、显示游标
声明游标
打开游标
提取游标
关闭游标
显示游标的属性:
%isopen
%rowcount-- 不同、
%found
%notfound
1、显示游标(不带参数)
声明游标:cursor 游标名 is select语句;
打开游标:open 游标
提取游标:fetch 游标名 into 变量1,变量2,…
关闭游标: close 游标名
(!SQLserver中还需要释放资源)
eg:
declare
cursor curStudent is select Sno,Sname from Student;
vSno Student.Sno%type;
vSname Student.Sname%type;
begin
open curStudent;
fetch curStudent into vSno,vSname;
while curStudent%found loop
dbms_output.put_line(‘学号:’||vSno||’,姓名:’||vSname);
fetch curStudent into vSno,vSname ;
end loop;
close curStudent;
end;
eg:修改上例
declare
cursor curStudent is select Sno,Sname from Student;
vSno Student.Sno%type;
vSname Student.Sname%type;
begin
open curStudent;
loop
dbms_output.put_line(‘从游标中已经提取的记录的条数为:’||curStudent%rowcount);
fetch curStudent into vSno,vSname ;
exit when curStudent%notfound;
dbms_output.put_line(‘学号:’||vSno||’,姓名:’||vSname);
end loop;
close curStudent;
end;
eg:
–显示选课成绩最高的n个学生的学号,课程号和成绩
declare
cursor curSC is select Sno 学号,Cno,Grade from SC order by Grade desc;
vSC curSC%rowtype;–基于游标定义记录变量
begin
open curSC;
loop
fetch curSC into vSC;–使用记录变量来接收从游标中提取到的数据
exit when curSC%notfound or curSC%rowcount>&n;–改为全局变量
dbms_output.put_line(‘学号:’||vSC.学号||’,课程号:’||vSC.Cno||’,成绩:’||vSC.Grade);
end loop;
close curSC;
end;
2、显示游标(带参数)
eg:
–显示选课成绩最高的n个学生的学号,课程号和成绩
declare
cursor curStudent(vSdept char) is select Sno,Sname from Student where lower(Sdept)=lower(vSdept);
vSno Student.Sno%type;
vSname Student.Sname%type;
begin
open curStudent(‘is’);
loop
fetch curStudent into vSno,vSname ;–提取数据
exit when curStudent%notfound;
dbms_output.put_line(‘学号:’||vSno||’,姓名:’||vSname);–显示
end loop;
close curStudent;–关闭游标
end;
3、游标for循环
for记录变量名 in 游标名 loop
…
end loop;
–eg:
declare
cursor curSC is select Sno,Cno,Grade from SC order by Grade desc;
begin
for vSC in curSC loop
dbms_output.put_line(‘学号:’||vSC.Sno||’,课程号:’||vSC.Cno||’,成绩:’||vSC.Grade);
exit when curSC%notfound or curSC%rowcount=&n;–改为替代变量
end loop;
end;