refactor:@@→$$に変更、一時テーブルのテーブル名修正、DEALLOCATE追加
This commit is contained in:
parent
9fb7e706ec
commit
a7b2bb0c85
@ -1,5 +1,5 @@
|
||||
-- A5M2で実行時にSQL区切り文字を「;」以外にすること
|
||||
-- @@から始まる文字は後からREPLACEする文字を示す独自ルール
|
||||
-- $$から始まる文字は後からREPLACEする文字を示す独自ルール
|
||||
CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255))
|
||||
BEGIN
|
||||
-- 例外処理
|
||||
@ -8,16 +8,25 @@ BEGIN
|
||||
BEGIN
|
||||
GET DIAGNOSTICS CONDITION 1
|
||||
@error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT;
|
||||
DROP TEMPORARY TABLE IF EXISTS make_history_tmp;
|
||||
EXECUTE drop_tmp_table;
|
||||
DEALLOCATE PREPARE drop_tmp_table;
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state;
|
||||
END;
|
||||
|
||||
-- ②-3で利用する一時テーブル削除のSQLを準備
|
||||
-- 例外処理でも利用するため先に記述
|
||||
SET @drop_tmp_table = '
|
||||
DROP TEMPORARY TABLE IF EXISTS make_history_tmp_$$target_table
|
||||
';
|
||||
SET @drop_tmp_table = REPLACE(@drop_tmp_table, "$$target_table", target_table);
|
||||
PREPARE new_history_save_stmt from @drop_tmp_table;
|
||||
|
||||
-- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する
|
||||
SET @new_history_save = '
|
||||
UPDATE @@target_table
|
||||
UPDATE $$target_table
|
||||
SET
|
||||
start_datetime = @@target_column
|
||||
start_datetime = $$target_column
|
||||
, end_datetime = "9999-12-31 00:00:00"
|
||||
, upd_user = CURRENT_USER()
|
||||
, upd_date = CURRENT_TIMESTAMP()
|
||||
@ -25,20 +34,21 @@ BEGIN
|
||||
start_datetime IS NULL
|
||||
AND end_datetime IS NULL
|
||||
';
|
||||
SET @new_history_save = REPLACE(@new_history_save, "@@target_table", target_table);
|
||||
SET @new_history_save = REPLACE(@new_history_save, "@@target_column", target_column);
|
||||
SET @new_history_save = REPLACE(@new_history_save, "$$target_table", target_table);
|
||||
SET @new_history_save = REPLACE(@new_history_save, "$$target_column", target_column);
|
||||
PREPARE new_history_save_stmt from @new_history_save;
|
||||
EXECUTE new_history_save_stmt;
|
||||
DEALLOCATE PREPARE ew_history_save_stmt;
|
||||
|
||||
-- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成
|
||||
SET @make_history_tmp_create = '
|
||||
CREATE TEMPORARY TABLE make_history_tmp
|
||||
CREATE TEMPORARY TABLE make_history_tmp_$$target_table
|
||||
SELECT
|
||||
id
|
||||
, MIN(@@target_column) AS min_start_datetime
|
||||
, MIN($$target_column) AS min_start_datetime
|
||||
, MAX(start_datetime) AS max_start_datetime
|
||||
FROM
|
||||
@@target_table
|
||||
$$target_table
|
||||
WHERE
|
||||
end_datetime = "9999-12-31 00:00:00"
|
||||
GROUP BY
|
||||
@ -46,15 +56,16 @@ BEGIN
|
||||
HAVING
|
||||
count(id) = 2
|
||||
';
|
||||
SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "@@target_table", target_table);
|
||||
SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "@@target_column", target_column);
|
||||
SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_table", target_table);
|
||||
SET @make_history_tmp_create = REPLACE(@make_history_tmp_create, "$$target_column", target_column);
|
||||
PREPARE make_history_tmp_create_stmt from @make_history_tmp_create;
|
||||
EXECUTE make_history_tmp_create_stmt;
|
||||
DEALLOCATE PREPARE make_history_tmp_create_stmt;
|
||||
|
||||
-- ②-2 「②-1」で取得した全件に更新処理を行う
|
||||
SET @update_end_datetime = '
|
||||
UPDATE @@target_table tt
|
||||
LEFT JOIN make_history_tmp mht
|
||||
UPDATE $$target_table tt
|
||||
LEFT JOIN make_history_tmp_$$target_table mht
|
||||
ON tt.id = mht.id
|
||||
AND tt.start_datetime = mht.min_start_datetime
|
||||
SET
|
||||
@ -64,12 +75,14 @@ BEGIN
|
||||
WHERE
|
||||
mht.id IS NOT NULL
|
||||
';
|
||||
SET @update_end_datetime = REPLACE(@update_end_datetime, "@@target_table", target_table);
|
||||
SET @update_end_datetime = REPLACE(@update_end_datetime, "@@target_column", target_column);
|
||||
SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_table", target_table);
|
||||
SET @update_end_datetime = REPLACE(@update_end_datetime, "$$target_column", target_column);
|
||||
PREPARE update_end_datetime_stmt from @update_end_datetime;
|
||||
EXECUTE update_end_datetime_stmt;
|
||||
DEALLOCATE PREPARE update_end_datetime_stmt;
|
||||
|
||||
-- ②-3 「②-1」で作成した一時テーブルを削除する
|
||||
DROP TEMPORARY TABLE make_history_tmp;
|
||||
EXECUTE drop_tmp_table;
|
||||
DEALLOCATE PREPARE drop_tmp_table;
|
||||
|
||||
END
|
||||
Loading…
x
Reference in New Issue
Block a user