pl/sql分匿名塊和命名塊


命名塊:存儲過程,函數,觸發器,包等



pl/sql語句塊分3部分:
(1)聲明部分
(2)可執行部分
(3)異常處理部分
其中可執行部分是語句塊中惟一要求必須存在的部分,聲明部分和異常處理部分是可選的。


1.匿名快語法:
declare
   declatation statements
begin
    executable statements
exception
    exception-handing statements
end
2.生成變量
語法:
變量名 [constant] datatype [not null] [:=value|default expr]
聲明變量方法1:

declare
v_first_name varchar2(50);
c_count   constant number :=0;  ---常量
v_hiredate  date;
v_valid   BOOLEAN NOT NULL DEFAULT TRUE;


聲明變量方法2:
declare
v_ename emp.ename%type;
v_sal emp.sal%type;
每個分句後用分號

conn scott/tiger
查詢 某個員工的姓名 薪水 稅後工資
set serveroutput on;
declare
v_ename varchar2(20);
v_sal   emp.sal%type;
c_tax_rate constant number(3,2):=0.06;
v_tax_sal number(6,2);
begin
select ename,sal into v_ename,v_sal from emp where empno=&no;
v_tax_sal:=c_tax_rate*v_sal;
dbms_output.put_line('v_ename: ' || v_ename);
dbms_output.put_line('v_sal: ' || v_sal);
dbms_output.put_line('v_tax_sal :'||v_tax_sal);
end;

3.標識符的命名規則

(1)定義變量,建議用v_,如v_ename
(2)定義常量,建議用c_,如c_tax_rate
(3)定義遊標,建議用_cursor做爲後綴,如emp_cursor
(4)定義表類型,建議用_table_type做爲後綴,如sal_table_type;
(5)定義表變量,建議用_table做爲後綴,例如sal_table;


4.PL/SQL編譯過程步驟

編譯過程包括語法檢查,綁定以及爲代碼生成。語法檢查涉及檢查PL/SQL代碼中的編譯錯誤。在糾正語法錯誤以後,會給每一個變量分配內存地址,以保存ORACLE數據,這個過程成爲綁定。接下來,會產生PL/SQL語句塊的僞代碼,僞代碼是PL/SQL引擎的指令列表,對於命名語句塊,僞代碼會存儲在數據庫中,並在程序下一次執行時使用。

5.替代變量
在匿名PL/SQL塊中接受輸入參數使用&或者&&做爲替代變量

6.初始化變量用select into語法
求emp表的平均工資
set serveroutput on;
declare
v_avg_sal emp.sal%type;
begin
select avg(sal) into v_avg_sal from emp;
dbms_output.put_line('v_avg_sal:'||v_avg_sal);
end;
#############################
7.if語法
if condition then
    statements;

[elsif condition then
    statements;]
[else
    statements;]

end if;

若是工資小於2000,把僱員的薪水加50
set serveroutput on;
DECLARE
v_sal emp.sal%TYPE;
begin
select sal into v_sal from emp where upper(ename)=upper('&ename');
if v_sal<2000 then
update emp set sal=sal+50 where upper(ename)=upper('&ename');
end if;
end;


//若是job=PRESIDENT,sal=sal+200
job=MANAGER ,sal=sal+1000
other ,sal=sal+100
&empno

declare
v_job emp.job%type;
v_empno emp.empno%TYPE;
begin
v_empno:=&empno;
select job into v_job from emp where empno=v_empno;
if v_job='PRESIDENT' then
update emp set sal=sal+200 where empno=v_empno;
elsif v_job='MANAGER'then
update emp set sal=sal+1000 where empno=v_empno;
else
update emp set sal=sal+100 where empno=v_empno;
end if;
end;
或者:
declare
v_job emp.job%type;
v_empno emp.empno%TYPE;
begin
select job,empno into v_job, v_empno from emp where empno=&empno;
if v_job='PRESIDENT' then
update emp set sal=sal+200 where empno=v_empno;
elsif v_job='MANAGER'then
update emp set sal=sal+1000 where empno=v_empno;
else
update emp set sal=sal+100 where empno=v_empno;
end if;
end;
############################
8.循環
#loop循環:
create table tmp01(id int);
declare
i int :=1;
begin
loop
insert into tmp01 values (i);
commit;
exit when i >100;
i := i+1;
end loop;
end;

#while循環
drop table tmp01 purge;
create table tmp01(id int);
declare
i int:=1;
begin
while i<100
 loop
insert into tmp01 values(i);
i:=i+1;
end loop;
commit;
end;

#for循環
drop table tmp01 purge;
create table tmp01(id int);
begin
for i in 1..100
 loop
insert into tmp01 values(i);
end loop;
commit;
end;


#############case語句
//用替代變量輸入部門號,使用case語句判斷條件更新僱員工資
部門號爲10,僱員加薪10%
部門號爲20,僱員加薪8%
部門號爲30,僱員加薪15%
若是輸入其餘數字,則顯示「該部門不存在」

DECLARE
v_deptno EMP.DEPTNO%TYPE:=&deptno;
begin
case v_deptno
when 10 then
update emp set sal=sal*1.1 where deptno=v_deptno;
when 20 then
update emp set sal=sal*1.08 where deptno=v_deptno;
when 30 then
update emp set sal=sal*1.15 where deptno=v_deptno;
else
dbms_output.put_line(v_deptno||' department is not exists!');
end case;
end;

9.SQL遊標
當執行select,insert,update,delete時,oracle會爲SQL語句分配相應的上下文區(context area).oracle使用上下區解析並執行相應的SQL語句,而遊標就是指向上下文區的指針。
遊標包括隱式遊標和顯示遊標。其中隱式遊標也稱爲SQL遊標,專門用於處理select into,insert,update,delete語句。顯示遊標用於處理多行select語句。


SQL遊標屬性:sql%found,sql%notfound,sql%rowcount,sql%isopen等

(1)sql%isopen:用於肯定SQL遊標是否打開。
(2)sql%found:用於肯定SQL語句是否執行成功。
(3)sql%notfound:用於肯定SQL語句是否執行成功。
(4)sql%rowcount:sql語句所作用的總計行數


set serveroutput on;
declare
v_deptno emp.deptno%type := &no;

begin
update emp set sal = sal*1.05 where deptno=v_deptno;
if sql%notfound then
dbms_output.put_line('deptno is not exist.');
else
dbms_output.put_line('sql execute successful.');
end if;
dbms_output.put_line('have '|| sql%rowcount || ' row  change.');
end;

---習題
用替代變量輸入客戶名(不區分大小寫)和所在城市,並修改客戶所在城市,若是客戶不在,則顯示「該客戶不在」

create table customer(customer_id number not null,customer_name varchar2(50),city_name varchar2(50),constraint pk_customer primary key(customer_id));

insert into CUSTOMER VALUES(1,'guoyf','luoy'),(2,'wangxq','daok'),(3,'songsl','yic');
declare
v_cusname customer.customer_name%type:='&name';
v_city varchar2(50):='&city';
begin
update customer set city_name=v_city where upper(customer_name)=upper(v_cusname);
if sql%notfound then
dbms_output.put_line(v_cusname||' is not exist');
end if;
commit; ...提交之後遊標關閉
end;

sql

相關文章
相關標籤/搜索