一、資料庫部分
二、程式部分
一、資料庫部分
使用英文名稱
Comment 盡量不要使用
二、程式部分
proc_label: BEGIN
/*
/*
-- Program name: program_name.sql
-- INPUT 參數說明:
-- IN in_gamer_id .....
-- IN in_gamer_id .....
-- IN in_gamer_id .....
-- 程式說明:
-- 作者:
-- 日期:
OUT `out_ret_code` varchar(100) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci
*/
DECLARE v_program_name VARCHAR(100) DEFAULT "porgram_name.sql";
-- 這個變數是為了方便輸出訊息時,便於顯示程式名稱,方便偵錯。
DECLARE EXIT handler for sqlexception
BEGIN
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
ROLLBACK;
SET out_ret_code =(SELECT concat("RETURNED_SQLSTATE:",@p1 ," MESSAGE_TEXT:" ,@p2 ));
CALL system_log(CONCAT("sqlexception:",out_ret_code));
END;
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
ROLLBACK;
SET out_ret_code =(SELECT concat("RETURNED_SQLSTATE:",@p1 ," MESSAGE_TEXT:" ,@p2 ));
CALL system_log(CONCAT("sqlexception:",out_ret_code));
END;
DECLARE EXIT handler for sqlwarning
BEGIN
GET DIAGNOSTICS CONDITION 1
@p1 = RETURNED_SQLSTATE, @p2 = MESSAGE_TEXT;
ROLLBACK;
SET out_ret_code =(SELECT concat("RETURNED_SQLSTATE:",@p1 ," MESSAGE_TEXT:" ,@p2 ));
CALL system_log(CONCAT("sqlwarning:",out_ret_code));
END;
-- 判斷傳入參數是否正確
IF input_is_error() THEN
SET out_ret_code = CONCAT("ERROR: 幫別人註冊: CP not enough!",v_my_cp, " bunit_type:",in_bunit_type);
CALL system_log(out_ret_code);
LEAVE proc_label;
SET out_ret_code = CONCAT("ERROR: 幫別人註冊: CP not enough!",v_my_cp, " bunit_type:",in_bunit_type);
CALL system_log(out_ret_code);
LEAVE proc_label;
END IF;
/*
先處理參數正確性檢查
先處理不用寫資料庫的運算
最後要寫資料庫時,再
START TRANSACTION
COMMIT;
ROLLBACK;*/
START TRANSACTION;
/*
資料庫更新動作
*/
SET out_ret_code = "SUCCESS";
COMMIT;
END;
IF NOT EXISTS(SELECT * FROM People) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'OMG PANIC';
END IF;
MySQL Stored Procedure Cursor 的標準程式模板
DECLARE v_exit INT DEFAULT FALSE;
DECLARE cur_table CURSOR FOR
SELECT field1, field2 FROM table;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_exit = TRUE;
OPEN cur_table;
my_fetch: LOOP
FETCH cur_table
INTO v_field1, v_field2;
IF v_exit THEN
LEAVE my_fetch;
END IF;
<process code here...>
END LOOP my_fetch;
CLOSE cur_table;
** PHP 程式部分,要配合的程式碼:
EVENT_BUTTON_UPDATE_ONCLICK()
// --- Custom Code
$db = new clsDBconn_mysql();
$sql1 = "CALL add_new_proc(".
$db->ToSQL($_SESSION["ID"],ccsText) . ",".
$control->ctrl->GetValue() . "," .
$db->ToSQL($aaaaa,ccsText). "," .
"@ret_code)";
$db->query($sql1);
$sql_log = "CALL system_log(".$db->ToSQL($sql1, ccsText).")";
$db->query($sql_log);
$sql2 = "SELECT @ret_code";
$db->query($sql2);
$Result = $db->next_record();
global $Redirect;
$Redirect = "success.php?ret_code=".$db->f("@ret_code");
$db->close();
沒有留言:
張貼留言