plsql語(yǔ)句塊:
set serveroutput on; //打開(kāi)控制臺(tái)輸出的命令
語(yǔ)法:
declare
聲明部分
begin
//執(zhí)行部分
異常召嘶,事物,語(yǔ)句塊等
end;
變量的類(lèi)型
oracle變量類(lèi)型蓬抄,oracle數(shù)據(jù)類(lèi)型:integer varchar2等
自定義數(shù)據(jù)類(lèi)型
1. 定義和列的類(lèi)型保持一致
v_sal emp.sal%type;-- 和emp表sal列的類(lèi)型保持一致
2. 定義和表達(dá)類(lèi)型保持一致
v_emp emp%rowtype; -- 和emp表的結(jié)構(gòu)一致
3. 定義自己的封裝類(lèi)(對(duì)象)
--聲明的是類(lèi)型
type type_emp_name_sal is record(v_empname EMP.ENAME%type,v_empsal EMP.SAL%type);
--變量名是v_name_sal 類(lèi)型是type_emp_name_sal
v_name_sal type_emp_name_sal;
4. 數(shù)組
--聲明數(shù)組類(lèi)型
type int_array is table of integer index by BINARY_integer;
--int類(lèi)型數(shù)組的變量
v_numbers int_array;
流程控制語(yǔ)句
1. if語(yǔ)句
if v_id = 1 then
dbms_output.put_line(v_id);
elsif v_id = 2 then
dbms_output.put_line('elsif');
else
dbms_output.put_line(v_id);
end if;
?
2. switch語(yǔ)句? case 語(yǔ)句
?
case
when v_id = 1 then
dbms_output.put_line(v_id);
when? v_id = 2 then
dbms_output.put_line('elsif');
else
dbms_output.put_line(v_id);
end case;
3. 循環(huán)語(yǔ)句
1. for循環(huán)
-- for循環(huán)
for v_i in reverse 1..10 loop
SYS.DBMS_OUTPUT.PUT_LINE(v_i);
end loop;
?
2. while循環(huán)
--while循環(huán)
while v_id < 10 loop
SYS.DBMS_OUTPUT.PUT_LINE(v_id);
--條件的改變
v_id := v_id + 1;
end loop;
3. loop循環(huán)
loop
SYS.DBMS_OUTPUT.PUT_LINE(v_id);
--條件的改變
v_id := v_id + 1;
exit when v_id = 10;
end loop;
4. 通過(guò)goto語(yǔ)句完成循環(huán)
<>
SYS.DBMS_OUTPUT.PUT_LINE(v_id);
v_id := v_id + 1;
if v_id < 10 then
goto a;
end if;
輸出菱形星號(hào)
*
***
*****
***
*
代碼:
declare
kong integer := 0;
xing integer :=0;
begin
for i in 1..5 loop
--每行由空格和星號(hào)組成
if i < 4 then
-- 1. 上半
kong := 3 - i;
xing := 2 * i - 1;
else
-- 2.下半
kong := i - 3;
xing := -2 * i + 11;
end if;
--輸出空格
for k in 1..kong loop
dbms_output.put(' ');
end loop;
--輸出星號(hào)
for k in 1..xing loop
dbms_output.put('*');
end loop;
--換行
dbms_output.new_line();
end loop;
end;
10個(gè)人圍成圈染厅,數(shù)到3退出圈,問(wèn)最后退出的是誰(shuí)
//數(shù)數(shù)問(wèn)題
declare
v_personNumber integer := 10;--人數(shù)
v_number integer := 3;--數(shù)的數(shù)
--int 類(lèi)型的數(shù)組
type int_array is table of integer index by BINARY_integer;
v_data int_array;
fang integer := 0;--放數(shù)據(jù)的下標(biāo)
qu? integer := 0;--取數(shù)據(jù)的下標(biāo)
numbers integer := 0;--存放數(shù)據(jù)的個(gè)數(shù)
v_length integer := v_personNumber;--隊(duì)列的大小
v_count integer := 0;--計(jì)數(shù)器
v_person integer := 0;--臨時(shí)存放出隊(duì)列的人
begin
-- 把人放入數(shù)組中
for i in 1..v_personNumber loop
v_data(fang) := i;
fang := fang + 1;
numbers := numbers + 1;
end loop;
-- 循環(huán)取數(shù)據(jù)判斷
while numbers <> 1 loop
--超過(guò)1個(gè)人弧腥,沒(méi)有退出圈
--出隊(duì)列
v_person := v_data(mod(qu,v_length));
qu := qu + 1;
numbers := numbers - 1;
--計(jì)數(shù)器加加
v_count := v_count + 1;
--判斷是否是v_number倍數(shù)
if mod(v_count,v_number) <> 0 then
--不是,添加到隊(duì)列
v_data(mod(fang,v_length)) := v_person;
fang := fang+1;
numbers := numbers + 1;
end if;
end loop;
--輸出結(jié)果
dbms_output.put_line(v_data(mod(qu,v_length)));
end;
plsql操作數(shù)據(jù)
分析: 對(duì)emp集體漲工資赏酥,漲幅不一致喳整, 1000以?xún)?nèi) 40%? 1000-2000 30% 2000-3000 20 3000 10%
declare
-- 數(shù)據(jù)全部取出
--數(shù)組存放數(shù)據(jù)(相當(dāng)于jdbc中的結(jié)果集封裝)
--員工編號(hào)和員工的薪水
type type_empno_sal is record (v_empno EMP.empno%type,v_sal EMP.SAL%type);
--定義員工數(shù)組
type type_emps_list is table of type_empno_sal index by binary_integer;
--定義保存員工信息的容器
v_emps_no_sals type_emps_list;
v_rows integer;--存放多少條數(shù)據(jù)
begin
select count(1) into v_rows from emp;
--去員工表每行信息,存儲(chǔ)到容器中
for r in 1..v_rows loop
select empno,sal into v_emps_no_sals(r - 1) from (select rownum num,emp.* from emp) e where? e.num = r;
end loop;
for r in 1..v_rows loop
case
when v_emps_no_sals(r-1).v_sal <= 1000 then
update emp set sal = sal * 1.4 where empno = v_emps_no_sals(r-1).v_empno;
when v_emps_no_sals(r-1).v_sal > 1000 and? v_emps_no_sals(r-1).v_sal <= 2000 then
update emp set sal = sal * 1.3 where empno = v_emps_no_sals(r-1).v_empno;
when v_emps_no_sals(r-1).v_sal > 2000 and? v_emps_no_sals(r-1).v_sal <= 3000 then
update emp set sal = sal * 1.2 where empno = v_emps_no_sals(r-1).v_empno;
else
update emp set sal = sal * 1.1 where empno = v_emps_no_sals(r-1).v_empno;
end case;
end loop;
commit;
end;
游標(biāo)
1.聲明游標(biāo)
2.打開(kāi)游標(biāo)
3.循環(huán)提前游標(biāo)
4.關(guān)閉游標(biāo)(釋放游標(biāo)占用的空間)
案例: 游標(biāo)實(shí)現(xiàn)降薪處理
declare
--聲明游標(biāo)
cursor v_emp_cur is select empno,sal from emp;
--信息封裝
type type_empno_sal is record (v_empno EMP.empno%type,v_sal EMP.SAL%type);
v_temp_empno_sal type_empno_sal;
begin
-- 打卡游標(biāo)
open v_emp_cur;
-- 循環(huán)提取游標(biāo)
loop
--去當(dāng)前游標(biāo)所在行的數(shù)據(jù)
fetch v_emp_cur into v_temp_empno_sal;
--判斷提取成功或失敗
if v_emp_cur%found then
--游標(biāo)有數(shù)據(jù)
-- SYS.DBMS_OUTPUT.PUT_LINE(v_temp_empno_sal.v_empno||'<>'||v_temp_empno_sal.v_sal);
case
when v_temp_empno_sal.v_sal > 3000 then
update emp set sal = sal * 0.6 where empno = v_temp_empno_sal.v_empno;
when v_temp_empno_sal.v_sal > 2000 and v_temp_empno_sal.v_sal<= 3000 then
update emp set sal = sal * 0.7 where empno = v_temp_empno_sal.v_empno;
when v_temp_empno_sal.v_sal > 1500 and? v_temp_empno_sal.v_sal <= 2000 then
update emp set sal = sal * 0.8 where empno = v_temp_empno_sal.v_empno;
else
update emp set sal = sal * 0.99 where empno = v_temp_empno_sal.v_empno;
end case;
else
--游標(biāo)提取完畢
exit;
end if;
commit;
end loop;
--關(guān)閉游標(biāo)
close v_emp_cur;
end;
案例2: for循環(huán)與游標(biāo)
declare
--聲明游標(biāo)
cursor cur_emp_no_sal(v_deptno emp.deptno%type) is select empno,sal from emp where deptno= v_deptno;
begin
--for循環(huán)提取游標(biāo)
for v_empno_sal in cur_emp_no_sal(10) loop
SYS.DBMS_OUTPUT.PUT_LINE(v_empno_sal.empno||'<>'||v_empno_sal.sal);
end loop;
end;
異常處理
根據(jù)異常名字exceptionwhen Too_many_rows then
SYS.DBMS_OUTPUT.PUT_LINE('Too_many_rows');
when others then
注意: others只能寫(xiě)在最后面
根據(jù)錯(cuò)誤代號(hào)exceptionwhen others then
case
when sqlcode=-1476 then
DBMS_OUTPUT.PUT_LINE('被0整除異常');
when sqlcode = -1422 then
DBMS_OUTPUT.PUT_LINE('返回多條記錄賦值');
else
DBMS_OUTPUT.PUT_LINE('其他異常');
end case;
--? SYS.DBMS_OUTPUT.PUT_LINE('出現(xiàn)異常了'||sqlcode||'<>'||sqlerrm);--? rollback;
自定義異常
declaremy_exec exception;my_exec2 exception;--把自定義異常和錯(cuò)誤代號(hào)綁定pragma EXCEPTION_INIT (my_exec2, -9527);beginif 3 > 2 then
--拋出異常 throw new 對(duì)象
raise my_exec2;
end if;
exception
when my_exec then
SYS.DBMS_OUTPUT.PUT_LINE('自定義異常'||sqlcode);
when my_exec2 then
SYS.DBMS_OUTPUT.PUT_LINE('自定義異常2<>'||sqlcode);
when others then
SYS.DBMS_OUTPUT.PUT_LINE('其他異常');
end;
4.自定義異常2
declare
my_exec exception;
my_exec2 exception;
pragma EXCEPTION_INIT (my_exec2, -9527);
begin
if 3 > 2 then
--拋出異常 throw new 對(duì)象 -20000 到 -20999
RAISE_APPLICATION_ERROR(-20000, '我的異常');
end if;
exception
when my_exec then
SYS.DBMS_OUTPUT.PUT_LINE('自定義異常'||sqlcode);
when my_exec2 then
SYS.DBMS_OUTPUT.PUT_LINE('自定義異常2<>'||sqlcode);
when others then
SYS.DBMS_OUTPUT.PUT_LINE('其他異常'||sqlcode);
end;