学习视频:https://www.bilibili.com/video/BV1tJ411r7EC?p=75 游标cursor:用于存放多条数据的容器。需要开始open和关闭close。游标下移使用“fetch...into...”
学习视频:https://www.bilibili.com/video/BV1tJ411r7EC?p=75
游标cursor:用于存放多条数据的容器。需要开始open和关闭close。游标下移使用“fetch...into...”。
declare
cursor myCursor is
select * from emp;
yb myCursor%rowtype;
begin
open myCursor;
for i in 1 .. 3 loop
fetch myCursor
into yb;
dbms_output.put_line(yb.empno || yb.ename);
end loop;
close myCursor;
end;
使用%found和%notfound属性判断游标是否有值,使用这两个属性的前提是游标必须经过下移。
declare
cursor myCursor is
select * from emp;
yb myCursor%rowtype;
begin
open myCursor;
loop
fetch myCursor
into yb;
dbms_output.put_line(yb.empno || "-" || yb.ename);
exit when myCursor%notfound;
end loop;
close myCursor;
end;
declare
cursor myCursor is
select * from emp;
yb myCursor%rowtype;
begin
open myCursor;
fetch myCursor
into yb; --游标必须先经过下移,才能使用%found和%notfound属性
while myCursor%found loop
fetch myCursor
into yb;
dbms_output.put_line(yb.empno || "-" || yb.ename);
end loop;
close myCursor;
end;
智能游标:变量定义、自动开关、自动下移。
--智能游标
--变量自动定义
--游标自动开
--游标自动下移
--自动关闭
declare
cursor myCursor is
select * from emp;
begin
for employee in myCursor loop
dbms_output.put_line(employee.empno || "-" || employee.ename);
end loop;
end;
异常:无法预测的程序错误。
declare
employee emp%rowtype;
begin
select * into employee from emp where empno = 70369;
exception
when no_data_found then
dbms_output.put_line("找不到数据");
when others then
dbms_output.put_line("默认异常,,,");
end;
存储过程:远程发送存储过程名,不需要发送具体的sql,避免发送过程中被截取、篡改sql。优点:提高效率,且更加安全。
create or replace procedure getmax(x number, y number) is
begin
if x > y then
dbms_output.put_line(x);
else
dbms_output.put_line(y);
end if;
end;
call getmax(10,12);
exec getmax(11,12);
execute getmax(45,56);
调用存储过程三种方法call、exec、execute。
存储过程参数模式:in表示只读,out表示只写,in out表示可读可写。
--参数变量 参数模式 参数类型。 默认模式为in。
create or replace procedure getmax(x in number, y in number,z out number) is
begin
if x > y then
z:=x;
else
z:=y;
end if;
end;
declare
max_result number;
begin
getmax(89, 85, max_result);
dbms_output.put_line(max_result);
end;
函数:和存储过程不同的是可以返回值。
create or replace function fgetmax(x number, y number) return number is
begin
if y > x then
return y;
else
return x;
end if;
end;
/
select fgetmax(45,44) from dual;
定时任务:使用dbms_job包创建定时任务。sumbit提交定时任务,run运行定时任务,任务编号是自动生成的,这个任务编号很重要,最好记录下来,删除定时任务需要任务编号。如果不知道编号是多少,只能手动找到dbms_job下的对应定时任务,然后移除。移除使用remove方法。
create table xperson(
id number primary key,
name varchar2(30)
)
create sequence seq_id;
create or replace procedure addxperson is
begin
insert into xperson values (seq_id.nextval, "bibi");
end;
declare
taskid binary_integer;
begin
dbms_job.submit(taskid,
"addxperson();",
sysdate,
"sysdate+10/(24*60*60)");
dbms_output.put_line(taskid);
dbms_job.run(taskid);
end;
触发器:某个事件引发些什么操作。DML操作,DDL操作,数据库的启动和关闭可以触发。加for each row表示行级触发器,每增删改一条数据,就触发一次,多条就多次。不加 for each row,则表示表级触发器
insert into xperson values(99,"bibi");
--不需要is 行级触发器,xperson表每增加一条数据,触发一次
create or replace trigger triggerone after insert on xperson
for each row
begin
dbms_output.put_line("数据添加了");
end;
--不需要is 行级触发器,xperson表每删除一条数据,触发一次
create or replace trigger triggerone after delete on xperson
for each row
begin
dbms_output.put_line("数据添加了");
end;
触发器的新旧数据获取: “:old.列名”,“:new.列名”。行级触发器才有新旧数据,表级触发器没有。insert操作只有新数据,没有旧数据。update操作有旧有新。
delete只有旧数据。
--行级触发器才有新旧数据
create or replace trigger triggerone after update on xperson
for each row
begin
dbms_output.put_line("就名字" || :old.name || " 新数据" || :new.name);
end;
update xperson set name = "bibibiib";
--结束END--
本文标题: Oracle(PLSQL)入门学习八(完结篇)
本文链接: https://www.lsjlt.com/news/5902.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0