在oracle中,如果乙個包含分隔符的字串(比如說「55*62*77」,這個字串是用*號做分隔符,可以拆分成三個數「55」,「62」和「77」),要將它們拆分出來比較麻煩,下面這個儲存過程就是為了方便對字串的分拆而寫的。
create or replace function getparamstr(
--字串分拆函式
i_srcstr varchar2, -- 包含引數的字串
i_sign varchar2, -- 引數分隔符號,如'~','?'
i_paramindex number, -- 欲分離的引數序號
o_paramstr out varchar2 -- 分離出來的引數內容
)return number
-- return:
-- 0.成功
-- 1. i_paramindex越界
-- 2. i_srcstr為空
-- 3. 無法定位引數序號
-- else. oracle error code
asv_len pls_integer;
v_startindex pls_integer;
v_endindex pls_integer;
err_srcstr_isnull exception;
err_paramindex_overflow exception;
err_startindex_notfound exception;
begin
if i_srcstr is null then
raise err_srcstr_isnull;
end if;
if i_paramindex <= 0 then
raise err_paramindex_overflow;
end if;
-- 初始化輸出引數
o_paramstr := '';
-- 取得引數內容的起始下標
if i_paramindex = 1 then
v_startindex := 0;
else
v_startindex := instrb(i_srcstr, i_sign, 1, i_paramindex - 1);
if v_startindex = 0 then
raise err_startindex_notfound;
end if;
end if;
-- 取得引數內容的結束下標
v_endindex := instrb(i_srcstr, i_sign, 1, i_paramindex);
if v_endindex = 0 then
v_endindex := lengthb(i_srcstr) + 1;
end if;
o_paramstr := substrb(i_srcstr, v_startindex + 1, v_endindex - v_startindex - 1);
-- 成功退出
return 0;
exception
when err_paramindex_overflow then
dbms_output.put_line('執行失敗:i_paramindex越界');
return 1;
when err_srcstr_isnull then
dbms_output.put_line('執行失敗:i_srcstr為空');
return 2;
when err_startindex_notfound then
dbms_output.put_line('執行失敗:無法定位引數序號');
return 3;
when others then
dbms_output.put_line(to_char(sqlcode)||' '||substrb(sqlerrm,1,200));
return sqlcode;
end getparamstr;
/這個是使用比較方便,如下:
create or replace procedure p_agt_addplan
(o_ret out number)is
v_count number (3);--這個是用來計數的
v_item varchar2(200);--這個用來儲存分拆後的那個字串
begin
v_count := 1;
--迴圈分拆字串55#66#77,將分拆出來的值放入v_item,如果迴圈結束,就返回非0數,所以就跳出迴圈了
while getparamstr('55#66#77','#',v_count,v_item) = 0 loop
dbms_output.put_line(v_item);
v_count := v_count + 1;
end loop;
commit;
o_ret := 0;
exception
when others then
rollback ;
o_ret := 1 ;
end p_agt_addplan;
/
字串和字串函式
字元輸入輸出 getchar putchar ch getchar putchar ch 字串函式 字串輸入 建立儲存空間 接受字串輸入首先需要建立乙個空間來存放輸入的字串。char name scanf s name 上述的用法可能會導致程式異常終止。使用字串陣列 可以避免上述問題 char na...
字串和字串函式
1.字串字面量 字串常量 用雙引號括起來的內容稱為字串字面量,也叫字串常量。字串常量屬於靜態儲存類別,這說明如果在函式中使用字串常量,該字串只會被儲存一次,在整個程式的生命期內存在,計時函式被呼叫多次。用雙引號括起來的內容被視為指向該字串儲存位置的指標。hello 中的 hello 類似於乙個陣列名...
字串函式
1 獲取字串的長度 length 2 判斷字串的字首或字尾與已知字串是否相同 字首 startswith string s 字尾 endswith string s 3 比較兩個字串 equals string s 4 把字串轉化為相應的數值 int型 integer.parseint 字串 lon...