兩個Oracle存儲過程程序分享

字號:


    為了解決日常工作中的問題,再加上好久沒有寫程序,利用這個周末的時間寫了如下的存儲過程或函數(shù),公布于此處,希望能對大家寫這一類的程序有所啟發(fā)。大家對寫程序可能有一個誤區(qū),有些人常說:你看我什么語言都會,比如說C,C++,VB,perl,shell,其實語言的本身并不重要,重要的還是在算法上,置于語法,用時去查一下就可以了。
    第一個存儲過程,屬性串替換函數(shù),常用于數(shù)據(jù)訂正過程中(使用oracle提供的replace函數(shù)會有問題)
    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;
    --跳出循環(huán)
    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;
    --對結(jié)果進行處理,去掉最左側(cè)的分號
    if substr(result_v,1,1)=';' then
    result_v := substr(result_v,2);
    end if;
    --返回結(jié)果
    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
    第二個存儲過程,檢查相關(guān)屬性對在目標屬性串是否存在,常用于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;
    --定義比較結(jié)果,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;