完成遊戲初始化的樣本庫的126筆樣本的寫入動作,所需的Package與程序init_guess。但這之後的24種排列組合,比較麻煩,因為PL/SQL陣列沒玩過。
這部分用來完成,把126筆資料寫入表格,為了避免遊戲解題過程中,被人為增刪樣本庫,增加了重設的簡單檢查動作。
CREATE OR REPLACE PACKAGE YAFUU168 AS
FUNCTION MAIN (V_TMP IN VARCHAR2 ) return varchar2;
PROCEDURE init_guess;
....其他
END yafuu168;
create or replace PACKAGE body YAFUU168 AS
FUNCTION MAIN (V_TMP IN VARCHAR2 ) RETURN VARCHAR2 IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
YAFUU168.INIT_GUESS;
....其他
return 'XXX';
END;
--
PROCEDURE INIT_GUESS IS
V_TMP VARCHAR2 ( 2000 ) :=
'0012346;0022367;0031236;0041489;0051679;0061479;0073456;0083789;0091567;0105678;0112359;0121238;' ||
'0131689;0143479;0151569;0163469;0173489;0183569;0191378;0201379;0213567;0225679;0231245;0242689;' ||
'0251356;0264568;0271579;0283478;0292379;0303468;0311457;0321359;0332569;0342479;0352459;0362467;' ||
'0372478;0381258;0391267;0402678;0412347;0421247;0434567;0441589;0451467;0462368;0475789;0483457;' ||
'0492458;0504679;0512348;0521289;0532345;0541278;0551678;0561789;0571257;0582468;0593578;0601578;' || '0613458;0625689;0631369;0642378;0653589;0661389;0672789;0681367;0692679;0702356;0713678;0721568;' ||
'0732456;0741246;0754789;0761269;0772457;0781358;0796789;0802579;0811478;0821259;0831249;0841237;' ||
'0854678;0861346;0872568;0881248;0894689;0902389;0911469;0922349;0931268;0942358;0952369;0961256;' ||
'0971456;0983689;0992589;1001279;1011468;1022567;1033679;1044569;1054589;1061458;1072357;1081347;' ||
'1094578;1101459;1111235;1123579;1132489;1142469;1151348;1161357;1171239;1181368;1191349;1201345;' ||
'1212578;1223459;1231234;1243467;1253568;1264579;';
V_CNT NUMBER;
BEGIN
SELECT COUNT ( * ) INTO V_CNT FROM YAFUU_GUESS_ARRAY;
--檢查樣本庫的筆數,確認是否正常。由於是非連續執行的運作方式,中途可能會被修改樣本。
IF V_CNT <> 126 THEN
--第一次執行、或資料庫被刪除、新增了,重新設定之。
DELETE FROM YAFUU_GUESS_ARRAY;
--拆解資料重新環原
FOR I IN 1 .. 126 LOOP
INSERT INTO YAFUU_GUESS_ARRAY
( SEQ_ID, GS01, GS02, GS03, GS04, GS_FLAG )
VALUES
( to_number ( SUBSTRB ( V_TMP, ( I - 1 ) * 8 + 1, 3 ) ),
TO_NUMBER ( SUBSTRB ( V_TMP, ( I - 1 ) * 8 + 4, 1 ) ),
to_number ( SUBSTRB ( V_TMP, ( I - 1 ) * 8 + 5, 1 ) ),
TO_NUMBER ( SUBSTRB ( V_TMP, ( I - 1 ) * 8 + 6, 1 ) ),
to_number ( SUBSTRB ( V_TMP, ( I - 1 ) * 8 + 7, 1 ) ),
1 ) ;
--DBMS_OUTPUT.put_line (to_number(SUBSTRB(V_TMP, (I-1) * 8 +1, 7));
END LOOP;
COMMIT;
END IF;
END;
END yafuu168;
接下來就是,把package寫好,以及用
select YAFUU168.main('xxx') from dual;進行每次的測試
第一次測試遇到
ORA-14551: 無法在查詢內執行 DML 作業
差點以為悲劇了,還好可以改正回來,用自治條例 PRAGMA AUTONOMOUS_TRANSACTION;
實測OK。
DELETE FROM YAFUU_GUESS_ARRAY;
select YAFUU168.main('xxx') from dual;
SELECT * FROM YAFUU_GUESS_ARRAY; --126
select YAFUU168.main('xxx') from dual;
SELECT * FROM YAFUU_GUESS_ARRAY; --跑兩次也是126筆
[開發技術組]哇咧~夠了(Oracle SQL)
各章節貼文
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/dev/1
[IT人生組] 鐵人不簡單, 挨踢人生刊, 卅天不間斷, 苦辣加甜酸。
各章節貼文
http://ithelp.ithome.com.tw/ironman6/player/yafuu168/life/1