有时一个很有趣的plsql的题,帖子见http://topic.csdn.net/u/20090813/19/bd1d75c1-2c75-44b3-9b42-824614ce0cf2.html?seed=549519025&r=59064250#r_59064250
一个算命的过程,接收输入的一个生日(yyyymmdd),每2位相加取个位,最后得到一位数字,此数字为偶数(命好),为质数(一般),否则为(命运悲惨).算法如下;
19850324
0735356
708881
78669
5425
967
53
8 -------------------偶数,命好
用plsql的实现
create or replace function suanming(birthday date) return varchar is
Result varchar(8);
v_birthstr varchar(8);
v_str varchar(8);
v_flag boolean := true;
begin
v_birthstr := to_char(birthday, 'yyyymmdd');
while (length(v_birthstr)>1)
loop
dbms_output.put_line(v_birthstr);
v_str := '';
for ind in 1..length(v_birthstr) loop
v_str := v_str||mod(to_number(substr(v_birthstr, ind, 1))+to_number(substr(v_birthstr, ind+1, 1)), 10);
end loop;
v_birthstr := v_str;
end loop;
dbms_output.put_line(v_birthstr);
if(mod(to_number(v_birthstr), 2) = 0) then
result := 'Good';
else
result := 'Bad';
end if;
return(Result);
end suanming;
SQL> set serverout on;
SQL> select suanming(sysdate) lucky from dual;
LUCKY
------------------------------------------------
Good
20090814
2099895
298774
17541
8295
014
15
6
SQL> select suanming(to_date('19850324', 'yyyymmdd')) lucky from dual;
LUCKY
-----------------------------------------------------------------------
Good
19850324
0735356
708881
78669
5425
967
53
8