plsql---dml操做,异常

DLM操做


update:
通常用 for update...的方式使用
update操做时,咱们要给他申请一个游标sql

SQL> declare
  2  cursor c1 is select sal from emp where deptno=10 for update;
  3  vc1 c1%rowtype;
  4  begin
  5  open c1;
  6  loop
  7  fetch c1 into vc1;
  8  exit when c1%notfound;
  9  update emp set sal=sal+1 where current of c1;
 10  end loop;
 11  close c1;
 12  commit;                     //必需要加上事务的操做
 13  end;
 14  /

PL/SQL procedure successfully completed.oracle




insert:
 app

SQL> begin 
  2  insert into emp(ename,empno) values('LLQ',8888);
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> ide


 

SQL> l
  1  declare
  2  vename emp.ename%type;
  3  vempno emp.empno%type;
  4  begin
  5  vename:='aaaa';
  6  vempno:='7777';
  7  insert into emp(ename,empno) values(vename,vempno);
  8  dbms_output.put_line(sql%rowcount||'  rows insert');
  9  commit;
 10* end;
SQL> /
1  rows insert

PL/SQL procedure successfully completed.oop




delete 在项目中建议锁表在操做,即update的方式fetch

SQL> begin
  2  delete emp where deptno=7777;
  3  commit;
  4  end;
  5  /

PL/SQL procedure successfully completed.

SQL> ui



select
 spa

DDL操做

--必须使用动态sql去建立
exexute immediate ‘ddl语句’; //动态的书写

create指针


SQL> begin
  2  execute immediate 'create table aa(name varchar2(10))';
  3  end;
  4  /

PL/SQL procedure successfully completed.索引



drop:
 

SQL> begin
  2  execute immediate 'drop table aa';
  3  end;
  4  /

PL/SQL procedure successfully completed.




index:

SQL> l
  1  begin
  2  for i in(select index_name from user_indexes where status='UNUSED')loop
  3  execute immediate 'alter index i.index_name rebuild';  //索引重建
  4  end loop;
  5* end;
SQL> /

PL/SQL procedure successfully completed.




每晚整理咱们的空间和索引

SQL> l
  1  begin
  2  for i in(select index_name from user_indexes where status='UNUSED')loop
  3  execute immediate 'alter index i.index_name rebuild';
  4  end loop;
  5  for a in (seletc table_name name from user_tables) loop
  6  execute immedidate 'alter table a.name enable row movement';
  7  execute immedidate 'alter table a.name shrink space';
  8  execute immedidate 'alter table a.name disable row movement';
  9  end loop;
 10* end;
SQL> /



delect

SQL> declare
  2  cursor c1 is select table_name name from user_tables where table_name like'%AA';
  3  vc1 c1%rowtype;
  4  begin
  5  open c1;
  6  loop
  7  fetch c1 into vc1;
  8  if c1%notfound then exit;
  9  end if;
 10  execute immediate 'drop table'||vc1.name;
 11  end loop;
 12  end;
 13  /

PL/SQL procedure successfully completed.



闪回
flashback

select :

静态:select <> into <> from <> where...
动态:execute immedaite 'select <> from <> where... into <>';

静态实例

SQL> l
  1  declare
  2  vsal number;
  3  begin
  4  select sal into vsal from emp where empno=7788;
  5* end;



动态实例:
 

SQL> l
  1  declare
  2  vsal number;
  3  begin
  4  execute immediate 'select sal from emp where empno=7788' into vsal;
  5  dbms_output.put_line(vsal);
  6* end;
SQL> /
3002

PL/SQL procedure successfully completed.




 

SQL> l
  1  declare
  2  vsal number;
  3  vempno number:=7788;
  4  begin
  5  execute immediate 'select sal from emp where empno=:1 ' into vsal using vempno; //用:1来进行占位
  6  dbms_output.put_line(vsal);
  7* end;



从外部赋一个值

 

SQL> l
  1  declare
  2  vsal number;
  3  vename emp.ename%type;
  4  vempno number:=&epo;
  5  begin
  6  execute immediate 'select sal,ename from emp where empno=:1' into vsal,vename using vempno;
  7  dbms_output.put_line(vsal);
  8* end;
SQL> /
Enter value for epo: 7788
old   4: vempno number:=&epo;
new   4: vempno number:=7788;
3002

PL/SQL procedure successfully completed.
 





从外部赋两个值

 

SQL> l
  1  declare
  2  vsal number;
  3  vename emp.ename%type;
  4  vempno number:=&epo;
  5  vjob varchar2(30):='CLERK';
  6  begin
  7  execute immediate 'select sal,ename from emp where empno=:1 and job:=2' into vsal,vename using vempno ,vjob;
  8  dbms_output.put_line(vsal||' '||vename);
  9* end;




 

SQL> declare
  2  vsal number;
  3  vename emp.ename%type;
  4  vempno number:=&emp;
  5  vjob varchar2(30):='MANAGER';
  6  d_s varchar2(100);
  7  begin
  8  d_s:='select sal,ename from emp where empno=:1 and job=:2';
  9  execute immediate d_s into vsal,vename using vempno,vjob;
 10  dbms_output.put_line(vsal||' '||vename);
 11  end;
 12  /



update:

SQL> declare
  2  vdeptno number;
  3  vsal number;
  4  begin
  5  update emp set sal=sal+1 where deptno=7566 return sal into vsal;
  6  dbms_outpu.put_line(vsal);
  7  end;
  8  /



insert:

静态

declare
vsal number;
begin
insert into dept values(11,'aa','bb');
end;



动态
 

declare
v1 number:=&1;
v2 varchar2(30):=&2;
v3 varchar2(30):=&3;
begin
insert into dept values(:1,:2,:3) into v1,v2,v3;
end;






返回多行:
pl/sql 表:
索引plsql表---可自动增加,index integer 经过指针的移动取数据
嵌套plsql表---已不用
bulk collect into --集合插入/多行插入,经过表的编号去取数据

声明方法
type <> is table of <number>| index integer;


first :表示plsql里的第一条
next:表示plsql里的第一条
count:表示有多少行
last:表示plsql里的第一条
limit:数据有没有被溢出
 

SQL> l
  1  declare
  2  type vtab is table of number;
  3  v1 vtab;
  4  begin
  5  execute immediate 'select sal from emp where deptno=10' bulk collect into v1;
  6  dbms_output.put_line(v1(1));  //v1.first 与v1(1)是同样的效果
  7  end;
  8*
SQL> /
2451

PL/SQL procedure successfully completed.

SQL> 



循环显示多行

SQL> l
  1  declare
  2  type vtab is table of number;
  3  v1 vtab;
  4  begin
  5  execute immediate 'select sal from emp where deptno=10' bulk collect into v1;
  6  for i in v1.first..v1.last loop              
  7  dbms_output.put_line(v1(i));
  8  end loop;
  9  end;
 10*
SQL> /
2451
5001
1301

PL/SQL procedure successfully completed.

SQL> 





循环的方式把表显示出来

SQL> l
  1  declare
  2  type vtab is table of dept%rowtype  index by binary_integer;
  3  v1 vtab;
  4  begin
  5  execute immediate 'select * from dept' bulk collect into v1;
  6  for i in 1..v1.count loop
  7  dbms_output.put_line(v1(i).deptno||' '||v1(i).dname);
  8  end loop;
  9  end;
 10*
SQL> /
50 DBA
4 SA
5 SA
6 SA
70 4G
80 IOS
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS

PL/SQL procedure successfully complete



静态游标

SQL> declare
2 cursor c1 is select * from emp where deptno=10;
3 vc1 c1%rowtype;
4 begin
5 open c1;
6 fetch c1 into vc1;
7 dbms_output.put_line(vc1);
8 end;
9 /

动态游标---优势是执行的时候才去定义游标

SQL> l
  1  declare
  2  type r1 is ref cursor ;
  3  v1 r1;
  4  vename varchar2(20);
  5  begin
  6  open v1 for 'select ename from emp where deptno=10';
  7  loop
  8  fetch v1 into vename;
  9  exit when v1%notfound;
 10  dbms_output.put_line(vename);
 11  end loop;
 12  close v1;
 13* end;
SQL> /
CLARK
KING
MILLER

PL/SQL procedure successfully completed.


 

SQL> l
  1  declare
  2  type r1 is ref cursor ;
  3  v1 r1;
  4  vename varchar2(20);
  5  vdeptno number;
  6  begin
  7  open v1 for 'select ename,deptno from emp where deptno=10';
  8  loop
  9  fetch v1 into vename,vdeptno;
 10  exit when v1%notfound;
 11  dbms_output.put_line(vename||' '||vdeptno);
 12  end loop;
 13  close v1;
 14* end;
SQL> /
CLARK 10
KING 10
MILLER 10

PL/SQL procedure successfully completed.



分支判断:
1.if ...then ...end if; //只有一个条件
2.if.....then...else......end if; //有两个条件
3.if.....then....elsif......then.......else.....end if; //超过三个条件

两个条件:

SQL> declare
  2  v1 number :=&n;
  3  begin
  4  if v1>1 then
  5  dbms_output.put_line('True');
  6  else
  7  dbms_output.put_line('False');
  8  end if;
  9  end;
 10  /
Enter value for n: 5
old   2: v1 number :=&n;
new   2: v1 number :=5;
True

PL/SQL procedure successfully completed.



三个条件:

SQL> l
  1  declare
  2  v1 number :=&n;
  3  begin
  4  if v1>1 then
  5  dbms_output.put_line('True');
  6  elsif v1=1 then
  7  dbms_output.put_line('ok');
  8  else
  9  dbms_output.put_line('False');
 10  end if;
 11* end;
SQL> /
Enter value for n: 1
old   2: v1 number :=&n;
new   2: v1 number :=1;
ok

PL/SQL procedure successfully completed.

SQL> 




分支:
法一:
case<条件表达式>
when 条件结果1 then
...
when 条件结果2 then
...
end case;

法二:
case
when <条件表达式1> then
...
when<条件表达式2> then
...
end case;

case实例:

SQL> l
  1  declare
  2  n number;
  3  hdate emp.hiredate%type;
  4  begin
  5  select hiredate into hdate from emp where empno=7566;
  6  case to_char(hdate,'yyyy')
  7  when '1981' then
  8  select count(*) into n from emp ;
  9  dbms_output.put_line(n);
 10  when '1982' then
 11  dbms_output.put_line(n);
 12  when '1987' then
 13  dbms_output.put_line(n);
 14  else
 15  dbms_output.put_line(n);
 16  end case;
 17* end;
SQL> /
15

PL/SQL procedure successfully completed.


 

SQL> l5
  5* when vasl<1500 then
SQL> c/vasl/vsal
  5* when vsal<1500 then
SQL> l
  1  declare
  2  vsal number:=&epo;
  3  begin
  4  case
  5  when vsal<1500 then
  6  update emp set sal=sal+1 where deptno=10;
  7  when vsal between 1500 and 3000 then
  8  update emp set sal=sal+2 where deptno=20;
  9  else
 10  null;
 11  end case;
 12* end;
SQL> /
Enter value for epo: 2000
old   2: vsal number:=&epo;
new   2: vsal number:=2000;

PL/SQL procedure successfully completed.


 


SQL> l
  1  declare
  2  cursor c1 is select sal from emp for update;
  3  vc1 c1%rowtype;
  4  begin
  5  open c1;
  6  loop
  7  fetch c1 into vc1;
  8  exit when c1%notfound;
  9  case
 10  when vc1.sal<1500 then
 11  update emp set sal=1500 where current of c1;
 12  when vc1.sal between 1500 and 3000 then
 13  dbms_output.put_line(vc1.sal);
 14  else
 15  null;
 16  end case;
 17  end loop;
 18* end;
SQL> /
1500
1600
1500
2977
1500
2850
2451
1500
1500
1500
1500

PL/SQL procedure successfully completed.




在匿名的plsql调用过程----匿名的plsql能够调用有名的,可是不能调用有名的
begin
exec <过程名>;
call <过程名>;
end;


exception---异常
一、预约义异常---在oracle内部定义好的,在用户使用过程当中遇到某种错误,由oracle内部自动引起的异常
(异常名,异常的代码,异常信息)

在dba用户下能够查看异常
>>>select text from dba_source where name='STANDARD' and text like '%EXCE%';

在oracle用户下能够用下面的命令查看咱们的错误
$oerr ora 1017 //1017为错误代码,action里面会告诉咱们处理的方法

二、非预约义的异常--不是由oracle引起,而由某种操做引起的异常,没有名字,能够屏蔽
(异常代码,异常信息,没有异常名)
e1 exception;
pragma exception_init(e1,942); --本身预约义异常

三、自定义异常---程序开发人员,在程序某种状况下抛出的异常
(-20000-----20999)
(没有异常信息,没有异常名,异常代码)

e1 exception;
pragma exception_init(e1,-20001);
raise e1; //抛出异常

屏蔽预约义的异常
exception
when...then...
when...then...

 

declare
v1 number:=0;
v2 number:=3;
begin
v1:=v2/v1;
exception
when zero_divide then
dbms_output.put_line('zero');
end;




自定义异常

SQL> declare
  2  vsal number;
  3  begin
  4  select sal into vsal from emp where deptno=10;
  5  exception
  6  when too_many_rows then
  7  dbms_output.put_line('too many');
  8  end;
  9  /
too many

PL/SQL procedure successfully completed.

SQL> 




定义非预约义的异常:

SQL> l
  1  declare
  2  e1 exception;                 //定义异常的名字
  3  pragma exception_init(e1,-00942);   //将异常名字与异常编号进行绑定
  4  begin
  5  execute immediate 'select * from d' ;   //必需要用动态的执行
  6  exception
  7  when e1 then
  8  dbms_output.put_line('table');
  9* end;
SQL> /
table

PL/SQL procedure successfully completed.




异常的定义和抛出方法:

法1:

SQL> l
  1  declare
  2  vsal number;
  3  e1 exception;
  4  pragma exception_init(e1,-20010);
  5  begin
  6  vsal :=&epo;
  7  if vsal<0 then
  8  raise e1;
  9  else
 10  dbms_output.put_line(vsal);
 11  end if;
 12  exception
 13  when e1 then
 14  dbms_output.put_line('ORA-20010 e1');
 15* end;
SQL> /
Enter value for epo: 20
old   6: vsal :=&epo;
new   6: vsal :=20;
20

PL/SQL procedure successfully completed.

SQL> /
Enter value for epo: -1
old   6: vsal :=&epo;
new   6: vsal :=-1;
ORA-20010 e1

PL/SQL procedure successfully completed.
 




法2: raise_application_error(异常代码,‘异常信息’);

SQL> l   1  declare   2  vsal number;   3  begin   4  vsal :=&epo;   5  if vsal<0 then   6  raise_application_error(-20001,'e1 not found');   7  else   8  dbms_output.put_line(vsal);   9  end if;  10* end; SQL> / Enter value for epo: 20 old   4: vsal :=&epo; new   4: vsal :=20; 20 PL/SQL procedure successfully completed. SQL> / Enter value for epo: -1                     //触发异常的条件 old   4: vsal :=&epo; new   4: vsal :=-1; declare * ERROR at line 1: ORA-20001: e1 not found ORA-06512: at line 6 SQL>   

相关文章
相关标签/搜索