SQL挑戰 如何高效生成編碼

2021-09-07 04:40:39 字數 3819 閱讀 5002

有這樣乙個需求:需要根據輸入的編碼(這個編碼值來自於資料庫的乙個表)生成下乙個編碼,編碼規則如下所示(我們暫且不關心這個邏輯是否合理,只關心如何實現):

1: 最小值為a0000, 最大值為zzzzz

2:編碼a0000的下乙個值為a0001, 編碼a9999的下乙個值為b0000, 編碼ab999的下乙個值為ac000,編碼ac999的下乙個值為ad000,依此規則內推。

3:不用擔心輸入值為類似a09bc這樣的值,應用程式會從表裡面取編碼的最大值。應用程式也會檢查、控制輸入引數,不用在資料庫的函式(function)裡面做檢查控制。

4:不用擔心輸入值為ac908這種值(大小寫問題),應用程式從表裡獲取編碼的值(不接受使用者輸入)。所以這個的檢查、控制也不用納入資料庫函式考慮範圍。

看到同事用ascii迴圈判斷字元是否為數字,大量的邏輯處理,我覺得並不是如何高效而且有些弄複雜了,寫了下面fun_gen_next_code,用正規表示式獲取數字部分,然後根據數字部分進行判斷處理。 寫完感覺也有點臃腫,因為要花大量的判斷處理邊界值(a9999 az999之類的邊界值),但是暫時也沒有更好的思路想法。 (oracle資料庫實現)

or replace function fun_gen_next_code(max_demension_no varchar2)

return varchar2
is
codevalue  number(5);
codechar   varchar(4);
charvalue  varchar2(5);
returncode varchar2(5);
begin
if length(max_demension_no) >=6 or length(max_demension_no) < 5 then
return

'';

end

if;

select regexp_substr(max_demension_no,'[[:digit:]]+') into codevalue from dual;
if length(codevalue)= 4 then
if codevalue= 9999 then
if substr(max_demension_no,1,1)='z'

then

charvalue :='za';
codechar := '000';
else
charvalue :=chr( ascii(substr(max_demension_no,1,1)) +1);
codechar := '0000';
end

if;

else
charvalue :=substr(max_demension_no,0,1);
codechar :=trim(to_char(codevalue+1,'0000'));
end

if;

returncode :=charvalue || codechar;
elsif length(codevalue)=3 then
if codevalue= 999 then
if substr(max_demension_no,1,2)='zz'

then

returncode :='zza' || '00';
else
if substr(max_demension_no,2,1) ='z'

then

returncode := chr( ascii(substr(max_demension_no,1,1)) +1) || '0000';
else
returncode :=substr(max_demension_no,1,1) || chr( ascii(substr(max_demension_no,2,1)) +1) || '000';
end

if;

end

if;

else
returncode :=substr(max_demension_no,1,1) || trim(to_char(codevalue+1,'000'));
end

if;

elsif length(codevalue)=2 then
if codevalue= 99 then
if  substr(max_demension_no,1,3) ='zzz'

then

returncode :='zzza0';
else
if substr(max_demension_no,3,1) ='z'

then

returncode := substr(max_demension_no,1,1) || chr( ascii(substr(max_demension_no,2,1)) +1) + '000';
else
returncode := substr(max_demension_no,1,2) || chr( ascii(substr(max_demension_no,3,1)) +1) || '00';
end

if;

end

if;

else
returncode :=substr(max_demension_no,1,3) + trim(to_char(codevalue+1,'00'));
end

if;

elsif length(codevalue)=1 then
if codevalue= 9 then
if substr(max_demension_no, 1,4) ='zzzz'

then

returncode := 'zzzza';
else
returncode := substr(max_demension_no, 1,3) || chr( ascii(substr(max_demension_no,4,1)) +1) || '0';
end

if;

else
returncode :=substr(max_demension_no,0,4) || trim(to_char(codevalue+1,'0'));
end

if;

else
if max_demension_no='zzzzzz'

then

returncode :='zzzzz';
else
returncode :=substr(max_demension_no, 1,4) || chr( ascii(substr(max_demension_no,5,1)) +1);
end

if;

end

if;

return returncode;
exception
when others
then
return ('');
end fun_gen_next_code;

Oracle 如何寫出高效的 SQL

start 要想寫出高效的sql 語句需要掌握一些基本原則,如果你違反了這些原則,一般情況下sql 的效能將會很差。連線資料庫是非常耗時的,雖然應用程式會採用連線池技術,但與資料庫互動依然很耗時,這就要求我們盡量用一條語句幹完所有的事,尤其要避免把sql 語句寫在迴圈中,如果你遇到這樣的人,應該毫不...

Oracle 如何寫出高效的 SQL

oracle 如何寫出高效的 sql 要想寫出高效的sql 語句需要掌握一些基本原則,如果你違反了這些原則,一般情況下sql 的效能將會很差。1.減少資料庫訪問次數 連線資料庫是非常耗時的,雖然應用程式會採用連線池技術,但與資料庫互動依然很耗時,這就要求我們盡量用一條語句幹完所有的事,尤其要避免把s...

如何生成隨機的唯一編碼

通常來講,oracle中生成隨機唯一編碼的方法就是呼叫sys guid 函式產生16進製制的16個字元的字串,如果用varchar2來儲存guid格式的字串,那就需要32個位元組,如果我們的編碼表的資料量很大,比如 的會員資訊表,其它的業務流水表會非常多地引用會員資訊表的主鍵,這個對儲存成本要求是非...