存储过程的创建语句如下:
create or replace function f_search_dist_by_addrid_1 (iaddr_id number)
return varchar2
is
v_dist_cd char(6);
v_prov_cd char(2);
v_sql varchar2(2000);
begin
v_prov_cd:=substr(iaddr_id,1,2);
v_sql:='select a.dist_cd
from tb_adr_street_'||v_prov_cd||' a,tb_adr_bldg_'||v_prov_cd||' b
where a.strt_id=b.strt_id
and b.dorplt_id='||iaddr_id||
'union
select a.dist_cd
from tb_adr_street_'||v_prov_cd||' a,tb_adr_bldg_'||v_prov_cd||' b,tb_adr_unit_'||v_prov_cd||' c
where a.strt_id=b.strt_id
and b.dorplt_id=c.dorplt_id
and c.unit_nbr_id='||iaddr_id||
'union
select a.dist_cd from tb_adr_specadr_'||v_prov_cd||' a
where a.addr_id='||iaddr_id||
'union
select a.dist_cd from tb_adr_addrline_'||v_prov_cd||' a
where a.addr_id='||iaddr_id;
begin
execute immediate v_sql into v_dist_cd;
exception
when no_data_found then
return '地址代码'||iaddr_id||'不存在!';
end;
if v_dist_cd is null then
return '地址代码为'||iaddr_id||'的行政区划代码不存在!';
else
return v_dist_cd;
end if;
end;
--------------------------------------------------------------------------
begin
execute immediate v_sql into v_dist_cd;
exception
when no_data_found then
return '地址代码'||iaddr_id||'不存在!';
end;
--------------------------------------------------------------------------
此块内容不加begin end输入正确的值提示错误无返回值。
此处加begin end和不加有什么区别?
还有这一块为什么一定要加begin end啊?
create or replace function f_search_dist_by_addrid_1 (iaddr_id number)
return varchar2
is
v_dist_cd char(6);
v_prov_cd char(2);
v_sql varchar2(2000);
begin
v_prov_cd:=substr(iaddr_id,1,2);
v_sql:='select a.dist_cd
from tb_adr_street_'||v_prov_cd||' a,tb_adr_bldg_'||v_prov_cd||' b
where a.strt_id=b.strt_id
and b.dorplt_id='||iaddr_id||
'union
select a.dist_cd
from tb_adr_street_'||v_prov_cd||' a,tb_adr_bldg_'||v_prov_cd||' b,tb_adr_unit_'||v_prov_cd||' c
where a.strt_id=b.strt_id
and b.dorplt_id=c.dorplt_id
and c.unit_nbr_id='||iaddr_id||
'union
select a.dist_cd from tb_adr_specadr_'||v_prov_cd||' a
where a.addr_id='||iaddr_id||
'union
select a.dist_cd from tb_adr_addrline_'||v_prov_cd||' a
where a.addr_id='||iaddr_id;
begin
execute immediate v_sql into v_dist_cd;
exception
when no_data_found then
return '地址代码'||iaddr_id||'不存在!';
end;
if v_dist_cd is null then
return '地址代码为'||iaddr_id||'的行政区划代码不存在!';
else
return v_dist_cd;
end if;
end;
--------------------------------------------------------------------------
begin
execute immediate v_sql into v_dist_cd;
exception
when no_data_found then
return '地址代码'||iaddr_id||'不存在!';
end;
--------------------------------------------------------------------------
此块内容不加begin end输入正确的值提示错误无返回值。
此处加begin end和不加有什么区别?
还有这一块为什么一定要加begin end啊?
你可以存储过程后面加一个 exception 的处理
但是if v_dist_cd is null then
return '地址代码为'||iaddr_id||'的行政区划代码不存在!';
else
return v_dist_cd;
end if;这一段代码就无法执行
这句有异常就可以跳到exception中执行如果没有这个块,这句执行出错,下面就执行不下去了
begin and是一个数据块,如果不加的执行语句是顺序执行,当遇到
错误就停止不能再执行下去了,如果加begin and 在其中加入
exception是为了捕获异常错误了。
BEGIN
EXCEPTION
END