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
--必須使用動態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>