plsql :procedure language
sql :structure query language
以下为一个简单的plsql:
declare
v_i number := 0 注:赋值符号为:= 或default
v_name varchar2(10) default 'a'
begin
insert into t values(v_i,v_name)
commit
end
条件语句:
1. if .... then
.....;
else
......;
end if ;
示例:
DECLARE
a number;
b varchar2(10);
BEGIN
a:=2;
if a = 1 then
b:='i am a';
elsif a =2 then
b:='i am b';
else
b:='i am c';
end if ;
dbms_output.put_line('b的值为:'||b);
END;
2. case
...
end case
示例:
DECLARE
a number;
b varchar2(10);
BEGIN
a:=2;
case
when a=1 then b:='A';
when a=2 then b:='B';
when a=3 then b:='C';
else
b:='others';
end case ;
dbms_output.put_line('b的值为:'||b);
END;
三种循环方式:
1、基本循环:
loop
end loop
示例:
DECLARE
x number;
BEGIN
x:=0;
loop
x:=x+1;
if x>=3 then
exit;
end if ;
dbms_output.put_line('内:x='||x);
end loop;
dbms_output.put_line('外:x='||x);
END;
示例2:
DECLARE
x number;
BEGIN
x:=0;
loop
x:=x+1;
exit when x>=3
dbms_output.put_line('内:x='||x);
end loop;
dbms_output.put_line('外:x='||x);
END;
2、while 条件 loop
...
end loop;
示例:
DECLARE
x number;
BEGIN
x:=0;
while x<=3
loop
x:=x+1;
dbms_output.put_line('内:x='||x);
end loop;
dbms_output.put_line('外:x='||x);
END;
3、for 循环
示例:
BEGIN
for i IN reverse 1 .. 5 (从大到小递增reverse,默认为从小到大)
loop
dbms_output.put_line('i='||i);
end loop;
dbms_output.put_line('end of for loop');
END;
SQL/PLS异常处理:
EXCEPTION + 系统、自定义异常
示例:
DECLARE
tname varchar2(10);
e exception ;
BEGIN
select dname INTO tname from dept where deptno='10';
if tname<>'B部门' then
RAISE e;
END IF;
DBMS_OUTPUT.PUT_LINE(tname);
EXCEPTION
WHEN e THEN
DBMS_OUTPUT.PUT_LINE('错误,不是需要的B部门');
END;
复合变量定义:
示例1:
DECLARE
type myrecord IS RECORD(
id varchar2(10),
name varchar2(10)
);
real_record myrecord;
BEGIN
select empno,ename INTO read_record FROM emp where empno = 001;
DBMS_OUTPUT.PUT_LINE(real_record.id||','||real_record.name)
;
END;
示例2:指定定义的变量类型与原表一样
DECLARE
type myrecord IS RECORD(
id varchar2(10),
name emp.ename%type 申明与对应表的字段一样
);
real_record myrecord;
BEGIN
select empno,ename INTO read_record FROM emp where empno = 001;
DBMS_OUTPUT.PUT_LINE(real_record.id||','||real_record.name)
;
END;
示例3:指定定义的的变量与整个表的字段及类型都一样ROWTYEP
DECLARE
myrec emp%ROWTYEP
BEGIN
select * INTO myrec FROM emp where empno = 001;
DBMS_OUTPUT.PUT_LINE(myrec.id||','||myrec.name);
END;
高级plsql应用:
游标(CURSOR):
示例1,显示游标:
DECLARE
cursor mycur is
select * from emp;
myrecord emp%ROWTYPE;
BEGIN
open mycur;
fetch mycur into myrecord;
while mycur%FOUND
loop
dbms_output.putline(myrecord.empno||','||myrecord.ename);
fetch mycur INTO myrecord;
end loop;
close mycur;
END;
示例2,带参数游标:
DECLARE
cursor cur_para(id varchar2) is 参数只需给类型,不给精度
select ename from emp where empno=id ;
t_name emp.ename%type;
BEGIN
open cur_para('0001');
loop
fitch cur_para into t_name;
exit when cur_para%NOTFOUND;
dbms_output.put_line(t_name);
end loop;
close cur_para;
END;
示例3,带参数游标2,for 循环:
DECLARE
cursor cur_para(id varchar2) is
select ename from emp where empno=id;
BEGIN
dbms_output.put_line('×××××结果集为:××××');
for cur in cur_para('001')
loop
dbms_output.put_line(cur.ename);
end loop;
END;
示例4,游标属性ISOPEN:
DECLARE
t_name emp.ename%type;
cursor cur(id varchar2) is
select ename from emp where empno=id;
BEGIN
if cur%isopen then
dbms_output.put_line('游标已经被打开');
else
open cur('0003');
end if ;
fetch cur into t_name;
close cur;
dbms_output.put_line(t_name);
END;
示例5,游标属性ROWCOUNT:
DECLARE
t_name varchar2(10);
cursor mycur is
select dname from dept;
BEGIN
open mycur
loop
fetch mycur into t_name;
exit when mycur%NOTFOUND or mycur%NOTFOUND is NUll;
dbms_output.put_line ('记录数为:'||mycur%ROWCOUTN);
end loop;
close mycur;
END;
示例6,使用游标修改数据:
DECLARE
cursor cur is
select dname from dept for update; 注意更改需添加for update
text varchar2(10);
BEGIN
open mycur
fetch cur into text;
while cur%FOUND
loop
update dept set dname=dname||'_t' where current of cur;
end loop;
close cur;
END;
存储过程(procedure):
示例1,创建一个简单的过程
CREATE or replace procedure myproc(id in varchar2)
is
name varchar2(10);
BEGIN
select ename into name from emp where empno=id;
dbms_output.put_line(name);
END myproc;
示例2,执行一个过程,带参数的存储过程:
DECLARE
tid varchar2(10);
BEGIN
tid:='0001';
myproc(tid);
END;
或:
BEGIN
myproc('0001');
END;
或
EXECUTE myproc('00001');
glogin 初始化文件:
set serveroutput on
本文转自pimg200551CTO博客,原文链接: http://blog.51cto.com/pimg2005/880492,如需转载请自行联系原作者