Oracle-day04 上
一、PL/SQL
(一)什么是 PL/SQL PL/SQL(Procedure Language/SQL)是 Oracle 对 sql 语言的过程化扩展,指在 SQL 命令语言中增加了过程处理语句(如分支、循环等),使 SQL 语言具有过程处理能力。把 SQL 语言的数据操纵能力与过程语言的数据处理能力结合起来,使得 PLSQL 面向过程但比过程语言简单、高效、灵活和实用。 基本语法结构 [declare --声明变量 begin --代码逻辑 [exception --异常处理 end; (二)变量 变量名 类型(长度); 变量赋值的语法:
需求: 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:=8012;--字数 --字数换算为吨数 v_usenum2:= round( v_usenum/1000,2); --计算金额 v_money:=round(v_price*v_usenum2,2); dbms_output.put_line(‘单价:‘||v_price||‘吨 数:‘||v_usenum2||‘金额:‘||v_money); end; Select into 方式 赋值
declare v_price number(10,2);--单价 v_usenum number;--水费字数 v_num0 number;--上月字数 v_num1 number;--本月字数 v_usenum2 number(10,2);--使用吨数 v_money number(10,2);--水费金额 begin --对单价进行赋值 v_price:=3.45; --变量赋值 select usenum,num0,num1 into v_usenum,V_num0,V_num1 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_price||‘吨数:‘ ||v_usenum2||‘金额:‘||v_money||‘上月字数:‘||v_num0||‘本月 字数‘||v_num1); end; (三)属性类型 作用:引用某表某列的字段类型 declare v_price number(10,2);--单价 v_usenum T_ACCOUNT.USENUM%TYPE;--水费字数 v_num0 T_ACCOUNT.NUM0%TYPE;--上月字数 v_num1 T_ACCOUNT.NUM1%TYPE;--本月字数 v_usenum2 number(10,2);--水费金额 begin --对单价进行赋值 v_price:=3.45; --v_usenum:=8090; select usenum,V_num1 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_price||‘吨数:‘ ||v_usenum2||‘金额:‘||v_money||‘上月字数:‘||v_num0||‘本月 字数‘||v_num1); end; %ROWTYPE --变量的用法-- declare v_price number(10,2);--单价 v_account T_ACCOUNT%ROWTYPE;--记录型 v_usenum2 number(10,2);--水费金额 begin --对单价进行赋值 v_price:=3.45; --赋值 select * into v_account from T_ACCOUNT where year=‘2012‘ and month=‘01‘ and owneruuid=1; --使用吨数 v_usenum2:= round(v_account.usenum/1000,2); --计算金额 v_money:=v_price*v_usenum2; DBMS_OUTPUT.put_line(‘单价:‘||v_price||‘吨数:‘ ||v_usenum2||‘金额:‘||v_money||‘上月字数: ‘||v_account.num0||‘本月字数‘||v_account.num1); end; (四)异常 预定义异常 - 当 PL/SQL 程序违反 Oracle 规则或超越系统限制时隐式 语法结构: exception when 异常类型 then 异常处理逻辑 根据上例中的代码,添加异常处理部分 --变量的用法-- declare v_price number(10,2);--水费单价 v_usenum T_ACCOUNT.USENUM%type; --水费字数 v_usenum2 number(10,3);--吨数 v_money number(10,2);--金额 begin v_price:=2.45;--水费单价 select usenum into v_usenum from T_ACCOUNT where owneruuid=1 and year=‘2012‘ and month=‘01‘; --字数换算为吨数 v_usenum2:= round( v_usenum/1000,3); --计算金额 v_money:=round(v_price*v_usenum2,2); dbms_output.put_line(‘单价:‘||v_price||‘吨 数:‘||v_usenum2||‘金额:‘||v_money); exception when NO_DATA_FOUND then dbms_output.put_line(‘未找到数据,请核实‘); when TOO_MANY_ROWS then dbms_output.put_line(‘查询条件有误,返回多条信息,请核实‘); end; (五)条件判断 if 条件 then 业务逻辑 end if; 基本语法 2 if 条件 then 业务逻辑 else 业务逻辑 end if; 基本语法 3 if 条件 then 业务逻辑 elsif 条件 then 业务逻辑 else 业务逻辑 end if; 需求:设置三个等级的水费 5 吨以下 2.45 元/吨 5 吨到 10 吨部分 3.45 元/吨 , declare v_price1 number(10,2);--不足 5 吨的单价 v_price2 number(10,2);--超过 5 吨不足 10 吨单价 v_price3 number(10,2);--超过 10 吨单价 v_account T_ACCOUNT%ROWTYPE;--记录型 v_usenum2 number(10,2);--水费金额 begin --对单价进行赋值 v_price1:=2.45; v_price2:=3.45; v_price3:=4.45; --赋值 select * into v_account from T_ACCOUNT where year=‘2012‘ and month=‘01‘ and owneruuid=1; --使用吨数 v_usenum2:= round(v_account.usenum/1000,2); --计算金额(阶梯水费) if v_usenum2<=5 then--第一个阶梯 v_money:=v_price1*v_usenum2; elsif v_usenum2>5 and v_usenum2<=10 then --第二个阶梯 v_money:=v_price1*5 + v_price2*( v_usenum2-5); else --第三个阶梯 v_money:=v_price1*5 +v_price2*5 + v_price3*( v_usenum2-10 ); end if; DBMS_OUTPUT.put_line(‘吨数:‘ ||v_usenum2||‘金额:‘||v_money||‘上月字数: ‘||v_account.num0||‘本月字数‘||v_account.num1); exception when NO_DATA_FOUND then DBMS_OUTPUT.put_line(‘没有找到数据‘); when TOO_MANY_ROWS then DBMS_OUTPUT.put_line(‘返回的数据有多行‘); end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |