变量声明部分(没有变量可以不写declare)
begin
执行部分
exception
异常处理部分
end
*:declare 和 exception部分是可选的
(1)PL/sql中变量的定义
格式1
变量名 变量类型 【约束】 default 默认值
格式2
变量名 变量类型 【约束】【:=初始值】
(2)一个简单的PL/sql块
declare
i number :=99;
begin
i:=i+10;
dbms_output.put_line(i);
end;
/
PL/sql块中流程控制
一。if语句
if 条件 then
。。。
end if;
if 条件 then
。。。
else
。。。
end if;
declare
zhangsan numberdefault 24;
lisi number :=28;
beginif zhangsan
dbms_output.put_line(‘zhangsan younger than lisi‘);
endif;
end;/
if 条件 then
...
elsif 条件 then
...
end if;
declare
age numberdefault 90;
height number :=175;
genderchar(2):=‘男‘;
beginif gender=‘男‘then
dbms_output.put_line(‘你可以和女性结婚‘);
endif;if height>170then
dbms_output.put_line(‘可以打篮球‘);elsedbms_output.put_line(‘可以踢足球‘);
endif;if age<20then
dbms_output.put_line(‘年轻小伙‘);
elsif age<= 50then
dbms_output.put_line(‘年轻有为‘);
elsif age<=70then
dbms_output.put_line(‘安享天伦‘);elsedbms_output.put_line(‘佩服佩服‘);
endif;
end;
二。While循环语句:
while 条件 loop
循环体
end loop;
declare
age numberdefault 1;
beginwhile age <=10loop
dbms_output.put_line(‘age is‘||age);
age:=age+1;
end loop;
end;
三。For循环结构:
for 循环变量 in [reverse] 起始值..终止值 loop
循环体
end loop;
beginfor i in 1..9loop
dbms_output.put_line(i);
end loop;for i in reverse 1..9loop
dbms_output.put_line(i);
end loop;
end;
四。Loop循环语句:
loop
循环体
exit when 条件;
end loop;
//计算1-100的和
declare
i number:=0;
total number:=0;
begin
loop
i:=i+1;
total:=total+i;if i=100then
exit;
endif;
end loop;
dbms_output.put_line(‘总和‘||total);
end;
跳出loop的方法
declare
i number :=0;
total number :=0;
begin
loop
i := i+1;
total := total +i;
exit when i>=100;
end loop;
dbms_output.put_line(‘总和‘||total);
end;
存储过程创建语法:
create or replace procedure 名称[(参数)]
authid current_user|definer --以定义者还是调用者的身份运行is[不要加declare]
变量声明部分
begin
主体部分
exception
异常部分
end;
create procedure a1isbegin
dbms_output.put_line(‘hello‘);
end;/
调用
Call a1();
create procedure a2(width int,height int)isareaint:=0;
begin
area:=width*height;
dbms_output.put_line(‘area is‘||area);
end;/
调用
Call a2(20,3);
oracle plsql的结构
标签:efi exit rev int 条件 变量 部分 number user
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉 本文系统来源:https://www.cnblogs.com/111wdh/p/13634608.html