From fd8f792961c4b5e7beb225a40c4c29ca05132f6c Mon Sep 17 00:00:00 2001 From: y-ono-r <95060536+y-ono-r@users.noreply.github.com> Date: Mon, 1 Aug 2022 20:02:29 +0900 Subject: [PATCH] =?UTF-8?q?feat:crm=5Fhistory=E3=81=AE=E6=96=B0=E8=A6=8F?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rds_mysql/stored_procedure/crm_history.sql | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 rds_mysql/stored_procedure/crm_history.sql diff --git a/rds_mysql/stored_procedure/crm_history.sql b/rds_mysql/stored_procedure/crm_history.sql new file mode 100644 index 00000000..cf2ca1d8 --- /dev/null +++ b/rds_mysql/stored_procedure/crm_history.sql @@ -0,0 +1,24 @@ +-- A5M2で実行時にSQL区切り文字を「;」以外にすること + +CREATE PROCEDURE crm_history(target_table VARCHAR(255), target_column VARCHAR(255)) +BEGIN + 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; + + 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'; + 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; + + 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 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); + PREPARE update_end_datetime_stmt from @update_end_datetime; + EXECUTE update_end_datetime_stmt; + + DROP TEMPORARY TABLE make_history_tmp; +END \ No newline at end of file