From 9a62a7eb5869afe60280b135e006835feecc04e0 Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Wed, 20 Jul 2022 09:01:48 +0900 Subject: [PATCH] =?UTF-8?q?fix:=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC?= =?UTF-8?q?=E6=8C=87=E6=91=98=E4=BF=AE=E6=AD=A3=E3=80=81=E4=BE=8B=E5=A4=96?= =?UTF-8?q?=E5=87=A6=E7=90=86=E3=81=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 54 +++++++++++++++++++--- 1 file changed, 48 insertions(+), 6 deletions(-) diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql index 7c183714..f9057a5e 100644 --- a/rds_mysql/stored_procedure/crm_history.sql +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -1,28 +1,70 @@ -- A5M2で実行時にSQL区切り文字を「;」以外にすること - CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) BEGIN + -- 例外処理 + -- エラーが発生した場合に一時テーブルの削除を実施 + DECLARE EXIT HANDLER FOR SQLEXCEPTION + BEGIN + GET DIAGNOSTICS CONDITION 1 + @error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT; + DROP TEMPORARY TABLE IF EXISTS make_history_tmp; + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = @error_msg, MYSQL_ERRNO = @error_state; + END; + -- ①-1 Salesforce側で更新されたデータの適用開始日時と適用終了日時を設定する - SET @new_history_save = 'UPDATE @@target_table SET start_datetime = @@target_column, end_datetime = "9999-12-31 00:00:00" WHERE start_datetime IS NULL AND end_datetime IS NULL'; + SET @new_history_save = ' + UPDATE @@target_table + SET + start_datetime = @@target_column + , end_datetime = "9999-12-31 00:00:00" + WHERE + 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); PREPARE new_history_save_stmt from @new_history_save; EXECUTE new_history_save_stmt; -- ②-1 Salesforce側で更新されたデータを検出用一時テーブルの作成 - SET @make_history_tmp_create = 'CREATE TEMPORARY TABLE make_history_tmp SELECT id, MIN(@@target_column) AS min_start_datetime, MAX(start_datetime) AS max_start_datetime FROM @@target_table WHERE end_datetime = "9999-12-31 00:00:00" GROUP BY id HAVING count(id) = 2'; + SET @make_history_tmp_create = ' + CREATE TEMPORARY TABLE make_history_tmp + SELECT + id + , MIN(@@target_column) AS min_start_datetime + , MAX(start_datetime) AS max_start_datetime + FROM + @@target_table + WHERE + end_datetime = "9999-12-31 00:00:00" + GROUP BY + id + 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_column", target_column); PREPARE make_history_tmp_create_stmt from @make_history_tmp_create; EXECUTE make_history_tmp_create_stmt; -- ②-2 「②-1」で取得した全件に更新処理を行う - SET @update_end_datetime = 'UPDATE @@target_table tt LEFT JOIN make_history_tmp mht ON tt.id = mht.id AND tt.start_datetime = mht.min_start_datetime SET start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND WHERE mht.id IS NOT NULL'; + SET @update_end_datetime = ' + UPDATE @@target_table tt + LEFT JOIN make_history_tmp mht + ON tt.id = mht.id + AND tt.start_datetime = mht.min_start_datetime + SET + start_datetime = mht.max_start_datetime - INTERVAL 1 SECOND + 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_column", target_column); PREPARE update_end_datetime_stmt from @update_end_datetime; EXECUTE update_end_datetime_stmt; -- ②-3 「②-1」で作成した一時テーブルを削除する DROP TEMPORARY TABLE make_history_tmp; + END \ No newline at end of file