一.创建一个函数
在PL/SQL Devoloper 中新建一个“function”函数,函数便自动带出开头与结尾”
create or replace function 函数名(name in type,name in type,name in type) return varchar2 is
FunctionResult varchar2;
begin
return(FunctionResult);
end 函数名;
例:函数名(name in type,..) 里的name in type 为入参值的名称,例如入参值的字段名为party_id,该入参值的字符类型为数值型,则为“函数名(party_id in number)。
二.为函数声明变量值
在begin之前声明变量的值 例:cc number(10) 便声名cc为数值型且长度不超过10的变量。
三.用if条件来判断并赋值
if...then...end if;
例:某一个字段的值与其中的值有相似的就给返回值赋值
if rec.cust_name like'未登记%' then
RET_MSG:='【提示】客户未实名校验,如用户办理业务请实名制或过户到实名客户'||chr(10)||RET_MSG;
end if;
四.用for循环来进行字段的的
for rec in(select 字段名1,字段名2,字段名3...from 表 where 条件) loop
end loop;
例:需要校验不同字段的值,与if搭配使用。
for rec in(
select ccu.cust_name,ccu.cust_type,ccu.region_id,ccu.status_cd,
(select max(cert_num)from party_cert_view pc where pc.party_id=ccu.party_id and pc.cert_type='1') x
from customer_view ccu
where ccu.party_id=dy_party_id
)loop
if rec.cust_name like'未登记%' then
RET_MSG:='【提示】客户未实名校验,如用户办理业务请实名制或过户到实名客户'||chr(10)||RET_MSG;
end if;
if rec.cust_name is null then`,,k
RET_MSG:='【异常】客户缺少名称,(cust.customer)中name字段为空,前台不能定位客户'||chr(10)||RET_MSG;
RETURN RET_MSG;
end if;
if rec.cust_type='1000'and rec.x is not null then
RET_MSG:='【提示】客户有身份证信息,但是判定为组织客户。用户如要办理业务,建议咨询用户后为用户办理新客户并过户到新客户后办理'||chr(10)||RET_MSG;
end if;
if rec.cust_type='1000' and rec.x is null then
RET_MSG:='【正常】客户未校验身份证信息判定为组织客户,注意办理业务需要用户提供单位证明或号码使用证明。用户如需要转成个人客户需要办理新客户并过户到新客户'||chr(10)||RET_MSG;
end if;
if rec.region_id='' then
RET_MSG:='【异常】客户地区为成都市(crm.party),可能导致订购销售品不可见'||chr(10)||RET_MSG;
RETURN RET_MSG;
end if;
if rec.status_cd is null then
RET_MSG:='【异常】客户状态为空(crm.party.party_status_cd),可能导致产品号码不可见'||chr(10)||RET_MSG;
RETURN RET_MSG;
end if;
if rec.region_id like'' then
RET_MSG:='【提示】客户地区非大成都范围,办理业务请注意销售品选择'||chr(10)||RET_MSG;
end if;
end loop;
需要注意:
1.赋给返回值的ret_msg:=‘’×××××× ”的是”:=“这样的符号。
2.code section的开始和结束都是有“begin ...end;”对应的。