-- A5M2で実行時に[SQL] - [スラッシュ(/)のみの行でSQLを区切る]に変えてから実行する
CREATE PROCEDURE src05.inst_merge_t_create()
SQL SECURITY INVOKER
BEGIN
	-- 例外処理
	DECLARE EXIT HANDLER FOR SQLEXCEPTION 
		BEGIN
			GET DIAGNOSTICS CONDITION 1
				@error_state = RETURNED_SQLSTATE, @error_msg = MESSAGE_TEXT;
			ROLLBACK;
			SIGNAL SQLSTATE '45000'
				SET MYSQL_ERRNO = @error_state, MESSAGE_TEXT = @error_msg;
		END;

	SET @error_state = NULL, @error_msg = NULL;

	TRUNCATE TABLE internal05.inst_merge_t;

	INSERT INTO
		internal05.inst_merge_t (
			dcf_dsf_inst_cd,
			dup_opp_cd,
			form_inst_name_kanji,
			form_inst_name_kana,
			inst_addr,
			prefc_cd
		)
	SELECT 
		dim.dcf_inst_cd,
		dim.dcf_inst_cd_new,
		ci.form_inst_name_kanji,
		ci.form_inst_name_kana,
		ci.inst_addr,
		ci.prefc_cd
	FROM 
		src05.dcf_inst_merge dim
	LEFT OUTER JOIN src05.com_inst ci
		ON dim.dcf_inst_cd_new = ci.dcf_dsf_inst_cd
	WHERE
			ci.delete_flg = '0'
		AND	src05.to_date_yyyymm01(dim.tekiyo_month) <= src05.get_syor_date() 
		AND	dim.muko_flg = '0'
		AND	dim.enabled_flg = 'Y';

END
