2009/04/22

PL/SQL - 身分證檢查

set serveroutput on; 
declare 
   myid varchar2(10):= 'A123456789'; --身分證號碼
   temp pls_integer;
   combine_char varchar2(9):= null;
  function get_prechar(v_pre in char) return number is
    type char_varray is varray(26) of char; 
    varray_char char_varray := char_varray('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','X','Y','W','Z','I','O');
  begin 
    for i in 1..varray_char.limit loop 
        if varray_char(i) = v_pre then 
           return i + 9;
        end if;
    end loop;    
  end get_prechar;

begin 
  --第一碼檢查
  temp := ascii(substr(myid,1,1)); 
  if temp <> 90 then
     dbms_output.put_line('第一碼非大寫英文字母');
  end if;
  
  --第二碼檢查
  temp := to_number(substr(myid,2,1));
  if temp not in (1,2) then 
     dbms_output.put_line('第二碼有誤');
  end if;
  
  temp := 0;
  combine_char := substr(get_prechar(substr(myid,1,1)),2,1) || substr(myid,2,8);
  for i in 1..9 loop 
      temp := temp + substr(combine_char,i,1) * (10-i);
  end loop;
  temp := temp +  to_number(substr(get_prechar(substr(myid,1,1)),1,1));
  if (mod(temp,10) = 10-substr(myid,10,1)) then 
     dbms_output.put_line('Valid');
  else 
     dbms_output.put_line('Invalid');
  end if;
  
exception 
   when others then 
      dbms_output.put_line('Invalid');
end;
相關連結 : 檢查規則

結論 : 寫得沒甚麼結構...><"

沒有留言: