1.基本语法结构
DECLARE
-- declaration statements] ① ----声明变量、赋值部分:变量是用来接收数据
BEGIN
-- executable statements ② -----正常部分
EXCEPTION
-- exception statements] ③ -----正常部分出现异常部分就会走异常
END;
2.变量
变量声明与赋值
declare
v_price number(10,2);--价格
v_usenum number;--水费字数
v_usenum2 number(10,2);--吨数
v_money number(10,2);--金额
begin
v_price := 2.45;--单价赋值
v_usenum := 9123;--水费字数
v_usenum2 := round(v_usenum/1000,2);--吨数
v_money := v_price * v_usenum2;--金额
dbms_output.put_line('金额'||v_money);--打印 put_line类似方法 dbms_output 包
end;
数据库拿取方式
select 列明 into 变量名
declare
v_price number(10,2);--价格
v_usenum number;--水费字数
v_usenum2 number(10,2);--吨数
v_money number(10,2);--金额
v_num0 number; --上月水表数
v_num1 number; --本月水表数
begin
v_price := 2.45;--单价赋值
--v_usenum := 9123;--水费字数
select usenum into v_usenum from t_account
where year='2012' and month = '01' and owneruuid = 1;
v_usenum2 := round(v_usenum/1000,2);--吨数
v_money := v_price * v_usenum2;--金额
dbms_output.put_line('水费字数'||v_usenum||',金额'||v_money);--打印 put_line类似方法 dbms_output 包
end;
3.属性类型
1.引用型
(表名.列名%type)
declare
v_price number(10,2);--价格
v_usenum t_account.usenum%type;--水费字数
v_usenum2 number(10,2);--吨数
v_money number(10,2);--金额
v_num0 t_account.num0%type; --上月水表数
v_num1 t_account.num1%type; --本月水表数
begin
v_price := 2.45;--单价赋值
--v_usenum := 9123;--水费字数
select usenum into v_usenum from t_account
where year='2012' and month = '01' and owneruuid = 1;
v_usenum2 := round(v_usenum/1000,2);--吨数
v_money := v_price * v_usenum2;--金额
dbms_output.put_line('水费字数'||v_usenum||',金额'||v_money);--打印 put_line类似方法 dbms_output 包
end;
补充说明(表名.列名%type)
2.记录型
(表名%type)
4.异常
自己定义错误的语句返回什么值:
5循环
1无条件循环
declare
v_num number;
begin
v_num :=1;
loop
v_num := v_num+1;
dbms_output.put_line(v_num);
if v_num>=100 then
exit;
end if;
end loop;
end;
2.有条件循环
declare
v_num number;
begin
v_num := 1;
while v_num < 200 loop
dbms_output.put_line(v_num);
v_num := v_num + 1;
end loop;
end;
3.for循环
declare
begin
for v_luo in 1 .. 50 loop
dbms_output.put_line(v_luo);
end loop;
end;