结构化查询语言pls/ql的基本单位是块,结构由三部分组成,声明部分、执行部分和异常处理部分。
[declare] ---声明部分,包括变量、常量、类型等
begin ---执行开始语言
--------- ---执行语句
[exception] ---异常处理部分
end; ---执行结束
例如: 表info(id,name,price)
declare
v_result number(10,2);
begin
select id into v_result from info where name='饮料';
dbms_output.put_line('结果为:'||v_result);
exception
when no_data_found then
dbms_output.put_line('没有找到数据');
end;
1.定义变量与常量
—type类型
declare
v_productid info.id%type; --%type声明变量,类型同表info.id的一致
v_productname varchar2(10);
v_produceprice number(10,2);
v_desperation constant v_productname%type :='测试'; --利用%type 引用v_productname的类型,并声明一个常量
v_date date :=sysdate;
begin
select id,name,price into v_productid,v_productname, v_produceprice
from info where name='饮料';
dbms_output.put_line('结果为:'||v_productid);
dbms_output.put_line('结果为:'||v_productname);
dbms_output.put_line('结果为:'||v_produceprice);
dbms_output.put_line('结果为:'||v_desperation);
dbms_output.put_line('结果为:'||v_date);
exception
when no_data_found then
dbms_output.put_line('没有找到数据');
end;
—record类型
declare
type product_rec is record --记录类型的声明
(
v_productid info.id%type, --%type声明变量,类型同表info.id的一致
v_productname varchar2(10),
v_produceprice number(10,2)
);
v_product product_rec;
begin
select id,name,price into v_product
from info where name='饮料';
dbms_output.put_line('结果为:'||v_product.v_productid);
dbms_output.put_line('结果为:'||v_product.v_productname);
dbms_output.put_line('结果为:'||v_product.v_produceprice);
exception
when no_data_found then
dbms_output.put_line('没有找到数据');
end;
–%rowpyte
declare
v_product info%rowtype;
begin
select * into v_product
from info where name='饮料';
dbms_output.put_line('结果为:'||v_product.id);
dbms_output.put_line('结果为:'||v_product.name);
dbms_output.put_line('结果为:'||v_product.price);
exception
when no_data_found then
dbms_output.put_line('没有找到数据');
end;
2.条件控制语句
—if..else..
declare
v_result number(10);
begin
v_result :=1;
if v_result>10 then
dbms_output.put_line('大于10:'||v_result);
elsif v_result=10 then
dbms_output.put_line('等于10:'||v_result);
elsif v_result<10 then
dbms_output.put_line('小于10:'||v_result);
end if;
end;
—case
declare
v_result number(10);
begin
v_result :=1;
case v_result
when '10' then
dbms_output.put_line('等于10:'||v_result);
when '11' then
dbms_output.put_line('等于11:'||v_result);
when '12' then
dbms_output.put_line('等于12:'||v_result);
else
dbms_output.put_line('其他:'||v_result);
end case;
end;
declare
v_result number(10);
begin
v_result :=1;
case
when v_result>10 then
dbms_output.put_line('大于10:'||v_result);
when v_result=10 then
dbms_output.put_line('等于10:'||v_result);
when v_result<10then
dbms_output.put_line('小于10:'||v_result);
else
dbms_output.put_line('其他:'||v_result);
end case;
end;
3.循环控制语句
—loop
declare
v_num number(10):=1;
begin
<<basic_loop>> --可以省略,结束时候直接 exit;
loop
dbms_output.put_line('结果是:'||v_num);
v_num:=v_num+1;
if v_num>100 then --可以使用 exit basic_loop when v_num>10;
exit basic_loop;
end if;
end loop;
end;
—for
declare
sum_i int:=0;
begin
for i in reverse 1..1000 loop -- reverse反向 表示i从100递减
sum_i:= sum_i+i;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;
declare
sum_i int:=1;
i int:=1;
begin
loop
i:=i+1;
sum_i :=sum_i +i;
exit when i =100;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;
—while
declare
sum_i int :=0;
i int :=0;
begin
while i<1000 loop
i:=i+1;
sum_i:=sum_i+i;
end loop;
dbms_output.put_line('前100个自然数的和是:'||sum_i);
end;