龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 数据库类 > Oracle 技术 >

两个Oracle存储过程程序分享

时间:2011-04-12 23:18来源:未知 作者:admin 点击:
分享到:
为了解决日常工作中的问题,再加上好久没有写程序,利用这个周末的时间写了如下的存储过程或函数,公布于此处,希望能对大家写这一类的程序有所启发。大家对写程序可能有一个

为了解决日常工作中的问题,再加上好久没有写程序,利用这个周末的时间写了如下的存储过程或函数,公布于此处,希望能对大家写这一类的程序有所启发。大家对写程序可能有一个误区,有些人常说:你看我什么语言都会,比如说C,C++,VB,perl,shell,其实语言的本身并不重要,重要的还是在算法上,置于语法,用时去查一下就可以了。

第一个存储过程,属性串替换函数,常用于数据订正过程中(使用oracle提供的replace函数会有问题)

create or replace function sp_replace_property_value(v_ch varchar2,v_from varchar2,

v_to varchar2) return varchar2
/*
creator:danchen
create_time:2008-4-19
function:replace taobao' property name and property value id as group
v_ch 属性串;v_from 源属性; v_to 目标属性,目标属性可为空,则变成删除属性
*/
as
--定义返回的返回的属性字符串
result_v varchar2(200):='';
--定义剩余属性字符串变量
temp_v varchar2(200):='';
--定义分号位置
fenhao_address number;
--定义临时属性对变量
v_pv varchar2(20);

begin
if v_ch is null or v_from is null then
return 'error';
end if;

if instr(v_ch,':') = 0 or instr(v_from,':')= 0 then
return 'error';
end if;

temp_v := v_ch;
loop
fenhao_address := instr(temp_v,';');
if fenhao_address=0 then
--没有找到分号,则为最后一组属性名:属性值
v_pv := temp_v;
--检查属性是否是要替换的属性
if v_pv != v_from then
result_v := result_v||';'||v_pv ;
else
if v_to is not null then
result_v := result_v||';'||v_to;
end if;
end if;
--跳出循环
exit;
else
--取出属性对
v_pv := substr(temp_v,1,instr(temp_v,';')-1);
--检查属性是否是要替换的属性
if v_pv != v_from then
result_v := result_v||';'||v_pv ;
else
if v_to is not null then
result_v := result_v||';'||v_to;
end if;
end if;
--得到剩余的属性对
temp_v := substr(temp_v,instr(temp_v,';')+1);
end if;        
end loop;

--对结果进行处理,去掉最左侧的分号
if substr(result_v,1,1)=';' then
result_v := substr(result_v,2);
end if;
--返回结果
return result_v;
end sp_replace_property_value;

 

第一个存储过程使用示例:

SQL> select sp_replace_property_value('33392:118167;33393:107054;33391:118167','33393:107054','') from dual;

SP_REPLACE_PROPERTY_VALUE('33392:118167;33393:107054;33391:118167','33393:107054

--------------------------------------------------------------------------------

33392:118167;33391:118167

SQL> select sp_replace_property_value('33392:118167;33393:107054;33391:118167','33393:107054','33393:100') from dual;

SP_REPLACE_PROPERTY_VALUE('33392:118167;33393:107054;33391:118167','33393:107054

--------------------------------------------------------------------------------

33392:118167;33393:100;33391:118167
第二个存储过程,检查相关属性对在目标属性串是否存在,常用于select查询语句(使用oracle提供的like查询会不准确)

create or replace function sp_exist_property(v_strpv varchar2,v_pv varchar2) return number
/*
creator:danchen
create_time:2008-4-20
function:检查v_pv在属性串v_strpv中是否全部存在
*/
as
type t_pvs is table of varchar2(50);
--保存分解后v_strpv
v_pvs t_pvs:=t_pvs();
--保存分解后v_pv
s_pvs t_pvs:=t_pvs();

--定义剩余属性字符串变量
last_pvs varchar2(200):='';
--临时属性变量
temp_pv varchar2(50);
--定义分号位置
fenhao_address number;


--定义比较结果,0不存在;1存在
v_check number;
v_result number;

begin
if (v_strpv is null) or (v_pv is null) then
return -1;
end if;

if instr(v_strpv,':')=0 or instr(v_pv,':')=0 then
return -2;
end if;


--分解v_strpv
last_pvs := v_strpv;
loop
fenhao_address := instr(last_pvs,';');
if fenhao_address=0 then
--没有找到分号,则为最后一组属性名:属性值;取出属性对
temp_pv := last_pvs;
v_pvs.extend;
v_pvs(v_pvs.last) := temp_pv;
--跳出循环
exit;
else
--取出属性对
temp_pv := substr(last_pvs,1,instr(last_pvs,';')-1);
--写入数组
v_pvs.extend;
v_pvs(v_pvs.last) := temp_pv;
--得到剩余的属性对
last_pvs := substr(last_pvs,instr(last_pvs,';')+1);
end if;        
end loop;


--分解v_pv
last_pvs := v_pv;
loop
fenhao_address := instr(last_pvs,';');
if fenhao_address=0 then
--没有找到分号,则为最后一组属性名:属性值;取出属性对
temp_pv := last_pvs;
s_pvs.extend;
s_pvs(s_pvs.last) := temp_pv;
--跳出循环
exit;
else
--取出属性对
temp_pv := substr(last_pvs,1,instr(last_pvs,';')-1);
--写入数组
s_pvs.extend;
s_pvs(s_pvs.last) := temp_pv;
--得到剩余的属性对
last_pvs := substr(last_pvs,instr(last_pvs,';')+1);
end if;        
end loop;

--检查是否存在
for i in 1..s_pvs.count loop
v_check := 0;
for j in 1..v_pvs.count loop
if v_pvs(j)=s_pvs(i) then
v_check := 1;
exit;
end if;
end loop;
--如果在本轮循环中,没有一次为真,则返回,不用再继续比较
if v_check=0 then
v_result := 0;
exit;
end if;
end loop;

--如果循环执行完
if v_check=1 then
v_result := 1;
end if;

--返回结果
return v_result;
end sp_exist_property;


第二个存储过程使用示例:

SQL> select sp_exist_property('33392:118167;33393:107054;33391:118167','1:118167') from dual;

SP_EXIST_PROPERTY('33392:118167;33393:107054;33391:118167','1:118167')

----------------------------------------------------------------------

0

SQL> select sp_exist_property('33392:118167;33393:107054;33391:118167','33393:107054') from dual;

SP_EXIST_PROPERTY('33392:118167;33393:107054;33391:118167','33393:107054')

--------------------------------------------------------------------------

1

SQL> select sp_exist_property('33392:118167;33393:107054;33391:118167','33392:118167;33393:107054') from dual;

SP_EXIST_PROPERTY('33392:118167;33393:107054;33391:118167','33392:118167;33393:1

--------------------------------------------------------------------------------

1

SQL> select sp_exist_property('33392:118167;33393:107054;33391:118167','33392:118167;33393:10705') from dual;

SP_EXIST_PROPERTY('33392:118167;33393:107054;33391:118167','33392:118167;33393:1

--------------------------------------------------------------------------------

0

SQL> select sp_exist_property('33392:118167;33393:107054;33391:118167','33392:118167;33391:118167') from dual;

SP_EXIST_PROPERTY('33392:118167;33393:107054;33391:118167','33392:118167;33391:1

--------------------------------------------------------------------------------

1

写程序是一种乐趣,平时应该多练练...

精彩图集

赞助商链接