PL/SQL编程基本概念
生活随笔
收集整理的這篇文章主要介紹了
PL/SQL编程基本概念
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*
=============================================================================pl/sql編程
=============================================================================*/--pl/sql塊的結(jié)構(gòu)
declare --聲明部門:在此聲明pl/sql用到的變量、類型以及游標,以及局部的存儲過程和函數(shù)
begin--執(zhí)行部分:過程及sql語句,即程序的組成部分
exception --異常處理部分:錯誤處理
end;--例子
create table toys
(
id number(20),
name varchar2(50),
price number(5,2),
sal_date date
)
insert into toys values(1,'張三',525,sysdate)
insert into toys values(2,'李四',525,'2016-05-06');
select * from toys;declare v_name varchar2(20);v_price number;
beginselect name,price into v_name,v_price from toys where id=1;dbms_output.put_line('名字:'||v_name||',價格'||v_price);
end;
10/*
type和rowtype
-------------------------------------------------
*/
declare -- v_name varchar2(25);-- v_name1 toys.name%type; --返回一個v_name1 toys%rowtype; --返回多個e_ronull exception;--聲明異常
begin select * into v_name1 from toys where id=1;dbms_output.put_line('名字:'||v_name1.name);
exception when dlp_val_on_index thendbms_output.put_line('將重復鍵插入id列');
end;--稅點
declare
v_start constant number:=3500; --聲明常量
begin
--sql語句
end--常量和變量的聲明變量名稱 pl/sql的數(shù)據(jù)類型(大小):=init_value;
eg:variable_name constant data_type:=value;--應用實例
declare v_ename varchar2(20);v_rate number(7,2);c_rate_incr constant number(7,2):=1.10;
begin--方法一:通過select into 賦值select ename,sal* c_rate_incr into v_ename,v_rate from employee where empno='7788';--方法二:通過賦值操作符“:=”給變量賦值v_ename:='scott';
end;--使用序列賦值v_no:=emp_seq.nextval;----------------------實例2---------------------------------
--根據(jù)員工編號查詢員工信息
declare v_empno employee.empno%type:=4;v_rec employee%rowtype;
beginselect * into v_rec from employee where empno=v_empno;dbms_output.put_line('姓名:'||v_rec.ename||'工資:'||v_rec.sal||'工作時間:'||v_rec.hiredate);end;
--==執(zhí)行成功之后,輸出:姓名:張四工資:10000工作時間:2017-02-02 00:00:00/*
----------------------pl/sql控制語句--------------------------------
*/
--if的語法
if <布爾表達式> thenpl/sql和sql語句
end if;
------------------------
if<布爾表達式> thenpl/sql和sql語句
else其他語句
end if;
-------------------------
if <布爾表達式> thenpl/sql語句和sql語句
elsif <其他布爾表達式> then其他語句
elsif <其他布爾表達式> then其他語句
else其他語句
end if;----注意:是elsif 不是elseif------------------------------case的語法--------------------------
-------格式一------
case 條件表達式when 條件表達式結(jié)果1 then語句段1when 條件表達式結(jié)果2 then語句段2when 條件表達式結(jié)果n then語句段n[else語句段]
end case;
-------格式二------
casewhen 條件表達式1 then語句段1when 條件表達式2 then語句段2when 條件表達式n then語句段n
else 語句段
end case;------------------------------循環(huán)控制--------------------------
loop 要執(zhí)行的語句;exit when <條件語句> --條件滿足時,退出循環(huán)語句
end loop;---while循環(huán)語句的語法
while <布爾表達式> loop 要執(zhí)行的語句;
end loop;
--for循環(huán)語句的語法
for 循環(huán)計數(shù)器 in [reverse] 下限 ...上限 loop要執(zhí)行的語句
end loop;------------------------------實例3-------------------------
declare v_counter number:=5;
begindbms_output.put_line('v_counter的當前值為:'||v_counter);if v_counter>=10 thennull;--為了使語法變得有意義,去掉null會報語法錯誤elsev_counter:=v_counter+10;dbms_output.put_line('v_counter 的改變值為:'||v_counter);end if;end;--========執(zhí)行成功之后輸出:v_counter的當前值為:5 v_counter 的改變值為:15/*=======================異常處理機制===============================
*/--語法
begin sequence_of_statements;
exception when <exception_name> thensequence_of_statements;when others thensequence_of_statements;
end;----------------------------實例4------------------------------------
--查詢編號為7788的雇員的福利補助(comm列)
declare v_comm employee.comm%type;e_comm_is_null exception ;--定義異常類型變量
begin select comm into v_comm from employee where empno=7788;if v_comm is null thenraise e_comm_is_null;end if;
exception when no_data_found thendbms_output.put_line('雇員不存在!錯誤為:'||sqlcode||sqlerrm);when e_comm_is_null thendbms_output.put_line('該雇員無補助');when others then dbms_output.put_line('出現(xiàn)其他異常!');end;
----================測試運行結(jié)果:雇員不存在!錯誤為:100ORA-01403: 未找到任何數(shù)據(jù)--自定義異常
raise_application_error(error_number,error_message);--實例
declare ....begin ....if v_com is null thenraise_application_error(-20001,'該雇員無補助');end if;
end;/*
====================================顯示游標================================
*/
--1.聲明游標
cursor cursor_name [(parameter [,parameter]...)]
[return return_type] is select_statement;
--2.打開游標
open cursor_name[(parameters)];
--3.提取游標
fetch cursor_name into variables;
--4.關(guān)閉游標
close cursor_name;--------------------實例6------------------------
declare name employee.ename%type;sal employee.sal%type; --定義兩個變量來存放ename和sal的內(nèi)容cursor emp_cursor is select ename,sal from employee;
beginopen emp_cursor;loopfetch emp_cursor into name,sal;exit when emp_cursor%notfound;dbms_output.put_line('第'||emp_cursor%rowcount||'個雇員:'||name|| 'oooo' || sal);end loop;close emp_cursor;
end;--===執(zhí)行成功輸出:
/*
第1個雇員:張一3000
第2個雇員:張二5000
第3個雇員:張三8000
第4個雇員:張四10000
第5個雇員:張五6300*/--使用顯示游標刪除或者更新
cursor cursor_name id select_statement for update [of columns];
--在使用for update 子句聲明游標時,可以使用下面語法更新行
update table_name set column_name=column_value where current of cursor_name;--根據(jù)編號查詢雇員的姓名
declare v_ename varchar2(20);
beginselect ename into v_ename from employee where empno=&empno;dbms_output.put_line('雇員的名字是:'||v_ename);
end;select * from employee;
總結(jié)
以上是生活随笔為你收集整理的PL/SQL编程基本概念的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑主流配置和中高端配置的区别?
- 下一篇: 通过BitDam的高级威胁保护来保护您的