n 游标
declare
cursor c is
select * from emp;
v_emp c%rowtype;
begin
open c;
loop
fetch c into v_emp;
exit when (c%notfound);
dbms_output.put_line(v_emp.ename);
end loop;
close c;
end;
----------------------
declare
cursor c is
select * from emp;
v_emp c%rowtype;
begin
open c;
fetch c into v_emp;
while (c%found) loop
dbms_output.put_line(v_emp.ename);
fetch c into v_emp;
end loop;
close c;
end;
-----------------
declare
cursor c is
select * from emp;
begin
for v_emp in c
loop
dbms_output.put_line(v_emp.ename);
end loop;
end;
n 带参数的游标
declare
cursor c (v_deptno emp.deptno%type, v_job emp.job%type)//声明形参
is
select ename, sal from emp where deptno = v_deptno and job = v_job;
begin
for v_temp in c(30,'CLERK') //v_temp不需要声明
loop
dbms_output.put_line(v_temp.ename);
end loop;
end;
n 可更新的游标
declare
cursor c
is
select * from emp2 for update;
begin
for v_temp in c loop
if (v_temp.sal < 2000) then
update emp2 set sal = sal * 2 where current of c;
elsif (v_temp.sal = 5000) then
delete from emp2 where current of c;
end if;
end loop;
commit;
end;
n 存储过程
存储过程出现编译错误,用show errors命令查看出错信息。
create or replace procedure p
is
cursor c
is
select * from emp2 for update;
begin
for v_temp in c loop
if (v_temp.deptno = 10) then
update emp2 set sal = sal + 10 where current of c;
elsif (v_temp.deptno = 20) then
update emp2 set sal = sal + 20 where current of c;
else
update emp2 set sal = sal + 50 where current of c;
end if;
end loop;
commit;
end;
--执行
exec p;
或
begin;
p;
end;
n 带参数的存储过程
//in代表该参数为传入参数,out代表该参数为传出参数。不写默认为in。
create or replace procedure p
(v_a in number, v_b number, v_ret out number, v_temp in out number)
is
begin
if (v_a > v_b) then
v_ret := v_a;
else
v_ret := v_b;
end if;
v_temp := v_temp + 1;
end;
--实验
declare
v_a number := 3;
v_b number := 4;
v_ret number;
v_temp number := 5;
begin
p(v_a, v_b, v_ret, v_temp);
dbms_output.put_line(v_ret);
dbms_output.put_line(v_temp);
end;
n 函数
create or replace function sal_tax(v_sal number) return number
is
begin
if (v_sal < 2000) then
return 0.10;
elsif (v_sal < 2750) then
return 0.15;
else
return 0.20;
end if;
end;
n 触发器
create table emp2_log
(
uname varchar2(20),
action varchar(10),
atime date
)
-----------
create or replace trigger trig
after insert or update or delete on emp2//触发条件
begin
if inserting then
insert into emp2_log values (USER, 'insert', sysdate);
elsif updating then
insert into emp2_log values (USER, 'update', sysdate);
elsif deleting then
insert into emp2_log values (USER, 'delete', sysdate);
end if;
end;
-----------
create or replace trigger trig
after insert or update or delete on emp2 for each row//每更新一行就会触发一次
begin
if inserting then
insert into emp2_log values (USER, 'insert', sysdate);
elsif updating then
insert into emp2_log values (USER, 'update', sysdate);
elsif deleting then
insert into emp2_log values (USER, 'delete', sysdate);
end if;
end;
-----------
用这种方法可以修改外键约束的字段。
create or replace trigger trig
after update on dept for each row
begin
//NEW代表操作后的记录,OLD代表操作前的记录
update emp2 set deptno = :NEW.deptno where deptno = :OLD.deptno;
end;
n 树状结构的存储和展示
create table article
(
id number primary key,
cont varchar2(4000),
pid number,--父id
isleaf number(1), --0代表非叶子节点,1代表叶子节点
alevel number(2)
)
-------------
insert into article values (1, '蚂蚁大战大象', 0, 0, 0);
insert into article values (2, '大象被打趴下了', 1, 0, 1);
insert into article values (3, '蚂蚁也不好过', 2, 1, 2);
insert into article values (4, '瞎说', 2, 0, 2);
insert into article values (5, '没有瞎说', 4, 1, 3);
insert into article values (6, '怎么可能', 1, 0, 1);
insert into article values (7, '怎么没可能', 6, 1, 2);
insert into article values (8, '可能性是很大的', 6, 1, 2);
insert into article values (9, '大象进医院了', 2, 0, 2);
insert into article values (10, '护士是蚂蚁', 9, 1, 3);
commit;
---------
蚂蚁大战大象
大象被打趴下了
蚂蚁也不好过
瞎说
没有瞎说
大象进医院了
护士是蚂蚁
怎么可能
怎么不可能
可能性是很大的
--------------------------
create or replace procedure p (v_pid article.pid%type, v_level binary_integer) is
cursor c is select * from article where pid = v_pid;
v_preStr varchar2(1024) := '';
begin
for i in 1..v_level loop
v_preStr := v_preStr || '****';
end loop;
for v_article in c loop
dbms_output.put_line(v_preStr || v_article.cont);
if (v_article.isleaf = 0)
then
p (v_article.id, v_level + 1);
end if;
end loop;
end;
推荐一个淘宝旗下网站,每天9块9包邮还返利。现在登录还有9元支付宝红包送你哦!http://invite.etao.com/67121619402a047e.htm