Java 开发中之五:PLSQL编程

pl/sql程序块:匿名

              declare ---定义变量(强语言的类型)

              begin

                 --执行语句(if while sql)

                 --DML(insert,delete,update)/TCL

                 --不能出来DDL/DCL

                 exception--异常处理程序             

             end;

 

declare
num number(4) default 1000;
esal number(4);
num1 number(5,2):=500.00;
pi constant number:=3.14;
eno emp.empno%type; --表示取那张表的中这个字段的类型
erow emp%rowtype; --行级变量 ,这个变量中包括很多变量

begin
num:='&num';--要求从键盘输入
select sal into esal from emp where empno='7369';
select * into erow from emp where empno='7369';
dbms_output.put_line('7369的工资是'||esal);
dbms_output.put_line(erow.ename||''||erow.deptno);
end;

注意:select 语句的使用:必存在Into,不能直接使用select

                                   返回的结果的要求(=1)

                                   匹配参数的个数

loop循环的使用

i:=0;
loop
exit when i=10;
dbms_output.put_line(i);
i:=i+1;
end loop;

for循环

for j in reverse 1..10
loop
dbms_output.put_line(j);
end loop;
end;

 

分支语句:

if then

  elsif then

        elsif then

    else 

end if;

 

实列:

declare
eno number;
edeptno number;
esal number;
begin
eno:=&eno;
select sal,deptno into esal,edeptno from emp1 where empno=eno;
if edeptno=10 then
if(esal*1.1>5000) then
update emp1 set sal=5000 where empno=eno;
else
update emp1 set sal=sal*1.1 where empno=eno;
end if;
elsif edeptno=20 then
if(esal*1.2>5000) then
update emp1 set sal=5000 where empno=eno;
else
update emp1 set sal=sal*1.2 where empno=eno;
end if;
elsif edeptno=30 then
if(esal*1.3>5000) then
update emp1 set sal=5000 where empno=eno;
else
update emp1 set sal=sal*1.3 where empno=eno;
end if;
else
null;
end if;
end;

 

如果您在plsql中要使用DDL(drop,truncate) 或是DCL(grant)的方式如下:

declare
--定义DDL/DCL
s varchar2(1000);

--execute immediate s
begin
--s:='create table test1 as select * from emp1 where 1=0';
--execute immediate s;
execute immediate 'drop table test1';--意思是立即执行的意思
end;

 

 

 

PL/SQL异常

declare
erow emp%rowtype;
begin
select * into erow from emp where empno=70069;
dbms_output.put_line(erow.empno ||' '|| erow.job);
exception
when no_data_found then
dbms_output.put_line('没有此员工');
end;

 

declare
erow emp%rowtype;
begin
select * into erow from emp ;
dbms_output.put_line(erow.empno ||' '|| erow.job);
exception
-- when no_data_found then
when too_many_rows then
dbms_output.put_line('返回行太多');
end;

 

异常体系
---异常:key(sqlcode) value(sqlerrm)

--自定义异常结构
declare
eno number;
--定义异常
myex exception;
begin
eno:=&eno;
if eno>80 then
raise myex;--抛出异常
end if;
exception
when myex then
dbms_output.put_line(sqlcode||''||sqlerrm);
end;

 

--用自定义的异常把系统异常给替换掉
declare
erow emp%rowtype;
myex exception;
pragma exception_init(myex,-1422);--用自定义的异常把系统异常给替换掉
begin
select * into erow from emp;
exception
when myex then
dbms_output.put_line('行太多:'||sqlcode||''||sqlerrm);
end;

 


--用自定义的异常添加到系统异常
declare
erow emp%rowtype;
begin
select * into erow from emp;
exception
when others then
raise_application_error(-20001,'行太多');  说明:-20001是因为Oracle异常处理一般都在20000以内,所以自己添加的异常可以从20001开始
end;

 

开发中常用的几个异常

cursor_already_open :打开游标异常

timeout_on_resource:锁机制

 

 

 

posted on 2012-12-09 16:33  peter.peng  阅读(485)  评论(0)    收藏  举报