iT邦幫忙

DAY 25
2

哇咧~夠了(Oracle SQL)系列 第 22

廿五、用SQL Plus 設計個小遊戲之5((Oracle SQL 2013/10/10)

今天來處理歷史紀錄存放,在流程圖上那塊遇到不明異常時[Show History & Help],
一律顯示出來此部分的歷史資料,保留一些未來功能擴充
開個table 紀錄,此紀錄在每次重玩都需要重新清空,搭配init_guess合併使用,
先記錄SQL,等有環境在測試修正。

Create Table yafuu_guess_history
( seq_id number, 
 gs01 number,
 gs02 number,
 gs03 number,
 gs04 number,
 gs0a number,
 gs0b number,
 gs_match varchar2(2000), --這裡功能先保留,可供記錄當時猜測的可能名單
 remark varchar2(240)
 );
 
 create unique index yafuu_guess_history_n1
 on yafuu_guess_history (seq_id);

insert into yafuu_guess_history (seq_id, gs01, gs02, gs03, gs04, gs0a, gs0b, gs_match, remark)
values (1, 1, 2, 3, 4, 0, 1, null, null);
insert into yafuu_guess_history (seq_id, gs01, gs02, gs03, gs04, gs0a, gs0b, gs_match, remark)
values (2, 5, 6, 7, 8, 1, 1, null, null);

下面這段,後續要改合併到package內處理,先記錄SQL,等有環境在測試修正。
declare  --package body = show_history
   cursor c1 is 
      select seq_id, gs01, gs02, gs03, gs04, gs0a, gs0b
        from yafuu_guess_history
       order by seq_id;
   v_history  varchar2(2000);
begin
   v_history := '目前電腦猜測的紀錄如下:'||chr(13)||chr(10);
   for i in c1 loop
       v_history := v_history||i.seq_id||'.   '||i.gs01||i.gs02||i.gs03||i.gs04||'  結果: '||
                    i.gs0a||'A '||i.gs0b||'B'||chr(13)||chr(10);
   end loop;
   v_history := v_history||'===== End ======'||chr(13)||chr(10);
   --以上如果需要有玩過才顯示,自行增加條件

   v_history := v_history||''||chr(13)||chr(10);
   v_history := v_history||'Help.....'||chr(13)||chr(10);
   v_history := v_history||'NEW  = 重玩,每次重新玩都必須執行這個參數.'||chr(13)||chr(10);
   v_history := v_history||'EXIT = 離開.'||chr(13)||chr(10);

   --return v_history  --未來是Function
end;

init_guess 內要增加這句
delete from yafuu_guess_history;

今天先到此。鐵人官網怪怪地!

[開發技術組]哇咧~夠了(Oracle SQL)
各章節貼文
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/dev/1

[IT人生組] 鐵人不簡單, 挨踢人生刊, 卅天不間斷, 苦辣加甜酸。
各章節貼文
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/life/1


上一篇
廿四、用SQL Plus 設計個小遊戲之4((Oracle SQL 2013/10/09)
下一篇
廿六、用SQL Plus 設計個小遊戲之6-主流程(Oracle SQL 2013/10/11)
系列文
哇咧~夠了(Oracle SQL)28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
月半車甫
iT邦研究生 3 級 ‧ 2013-10-11 10:44:30

修改為package function

<pre class="c" name="code">FUNCTION SHOW_HISTORY return varchar2 IS --Add By yafuu 2013/10/10
  CURSOR C1 IS 
    select seq_id, gs01, gs02, gs03, gs04, gs0a, gs0b
      FROM YAFUU_GUESS_HISTORY
     ORDER BY SEQ_ID;
  v_history varchar2(2000);
BEGIN
  v_history := '目前電腦猜測的紀錄如下:'||chr(13)||chr(10);
  for i in c1 loop
    V_HISTORY := V_HISTORY||I.SEQ_ID||'. '||I.GS01||I.GS02||I.GS03||I.GS04||' 結果: '||
                 i.gs0a||'A '||i.gs0b||'B'||chr(13)||chr(10);
  END LOOP;
  v_history := v_history||'===== End ======'||chr(13)||chr(10);
  --以上如果需要有玩過才顯示,自行增加條件
  --
  V_HISTORY := V_HISTORY||''||CHR(13)||CHR(10);
  V_HISTORY := V_HISTORY||'Help.....'||CHR(13)||CHR(10);
  V_HISTORY := V_HISTORY||'NEW = 重玩,每次重新玩都必須執行這個參數.'||CHR(13)||CHR(10);
  v_history := v_history||'EXIT = 離開.'||chr(13)||chr(10);
  return v_history; --未來是Function
END;
月半車甫 iT邦研究生 3 級 ‧ 2013-10-11 10:46:27 檢舉

測試結果:

<pre class="c" name="code"> 1* select YAFUU168.main('&reply') "Guess my number.." from dual
SQL> /
輸入 reply 的值: ??
舊的 1: select YAFUU168.main('&reply') "Guess my number.." from dual
新的 1: select YAFUU168.main('??') "Guess my number.." from dual

Guess my number..
------------------------------------------------------------------------
目前電腦猜測的紀錄如下:
===== End ======

Help.....
NEW = 重玩,每次重新玩都必須執行這個參數.
EXIT = 離開.

SQL> /
輸入 reply 的值: New

Guess my number..
------------------------------------------------------------------------
My 1st guess is : 1234

SQL> /
輸入 reply 的值: ExiT

Guess my number..
------------------------------------------------------------------------
Bye!

SQL> /
輸入 reply 的值: 10

Guess my number..
------------------------------------------------------------------------
加油!男孩!.....

SQL> /
輸入 reply 的值: 40

Guess my number..
------------------------------------------------------------------------
Bingo!

我要留言

立即登入留言