oracle编程基本语法,oracle编程基础语法
oracle數(shù)據(jù)開發(fā)
編程結(jié)構(gòu): ?declare
[定義變量]
begin
[邏輯代碼]
exception
[捕獲異常]
end;
實(shí)例:
declare
a number:=1;
b number:=2;
c number;
begin
c:=(a*b)/(a+b);
dbms_output.put_line(c);
exception
when zero_divide then
dbms_output.put_line('除數(shù)不能為0');
end;
變量:
長(zhǎng)度不能超過(guò)30個(gè)字符不能含有空格,表的字段長(zhǎng)度也是;
由字母、0-9數(shù)字、下劃線、美元符號(hào)‘$’ 以及#號(hào)組成
必須由字母開頭
關(guān)鍵字能作為變量,如if 等
基礎(chǔ)數(shù)據(jù)類型
實(shí)例:
param number default 104;
%type變量
%type是聲明一個(gè)與指定的列名相同的數(shù)據(jù)類型;
優(yōu)點(diǎn):用戶不必查看表中的數(shù)據(jù)類型,即可確保數(shù)據(jù)類型與范圍與表中的數(shù)據(jù)類型一致;
如果對(duì)表中的數(shù)據(jù)結(jié)構(gòu)修改,對(duì)編寫的程序沒(méi)有影響,程序的變量會(huì)隨結(jié)構(gòu)變化而變化
缺點(diǎn):執(zhí)行的時(shí)候解析%type 需要查詢數(shù)據(jù)詞典,確定數(shù)據(jù)類型,可能會(huì)對(duì)性能產(chǎn)生影響
實(shí)例:
var_name emp.ename%type;
復(fù)合變量
%rowtype變量;
%rowtype聲明一個(gè)與指定的表名相同的行類型;相當(dāng)于定義一個(gè)和指定表相同的數(shù)據(jù)結(jié)構(gòu)踢類型
保存一行數(shù)據(jù)
實(shí)例:
row_employee emp%rowtype
自定義變量:
聲明格式
type record_name record(
file_name date_type [not null] [:=default_value],
...
file_namen date_type [not null] [:=default_value]
)
根據(jù)用戶需求定制數(shù)據(jù)變量,類似C語(yǔ)言的結(jié)構(gòu)體
實(shí)例:
type user_type record(
user_no number,
user_name nvarchar2(10)
);
user user_type;
分支
if..then.. 語(yǔ)句
if 條件 then
處理語(yǔ)句
end if;
if..then.. 語(yǔ)句
if 條件 then ?處理語(yǔ)句 ?else 處理語(yǔ)句 end if;
if..then.. 語(yǔ)句
if 條件 then 處理語(yǔ)句
elsif 處理語(yǔ)句
else
處理語(yǔ)句
end if;
case條件語(yǔ)句
case
when 條件 then 處理語(yǔ)句;
when 條件 then 處理語(yǔ)句;
when 條件 then 處理語(yǔ)句;
end;
if var_param > 0 then
dbms_output.put_line('var_param 的值大于0');
end if;
if var_param > 0 then
dbms_output.put_line('var_param 的值大于0');
else dbms_output.put_line('var_param 的值小于或等于0');
end if;
if var_param = 0 then
dbms_output.put_line('var_param 的值等于0');
elsif var_param = 1 then
dbms_output.put_line('var_param 的值等于1');
else
dbms_output.put_line('var_param 的值不等于1或0');
end if;
case
when var_param = 0 then dbms_output.put_line('var_param 的值等于0');
when var_param = 1 then dbms_output.put_line('var_param 的值等于1');
when var_param = 2 then dbms_output.put_line('var_param 的值等于2');
end
循環(huán)語(yǔ)句
loop
處理語(yǔ)句
exit when 退出循環(huán)條件
end loop
while 循環(huán)語(yǔ)句
while 循環(huán)條件
loop
處理語(yǔ)句
end loop;
for循環(huán)
for 變量 in 變量集合 loop
處理語(yǔ)句
end loop;
loop
var_param:=var_param+1 --自增
exit when var_param>10 --當(dāng)var_param的值大于10時(shí)推出循環(huán)
end loop;
while var_param <= 10 --當(dāng)var_param的值小于等于10時(shí)進(jìn)入循環(huán)
var_param:=var_param+1 --自增
end loop;
for var_param in 1..10 --當(dāng)var_param的值小于等于10時(shí)進(jìn)入循環(huán)
loop
var_param:=var_param+1 --自增
end loop;
總結(jié)
以上是生活随笔為你收集整理的oracle编程基本语法,oracle编程基础语法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 英伟达 CEO 黄仁勋:我们 5 年前把
- 下一篇: oracle存储过程深入,深入了解ora