-- ============================================================
-- erp_mp_mbom 表增加下发时间字段
-- 下发时间: issue_date，用于记录物料清单下发车间的时间
-- 生成日期: 2026-02-13
-- ============================================================

SET NAMES utf8mb4;

DROP PROCEDURE IF EXISTS `proc_add_column_if_not_exists`;

DELIMITER //
CREATE PROCEDURE `proc_add_column_if_not_exists`(
    IN p_table_name VARCHAR(64),
    IN p_column_name VARCHAR(64),
    IN p_column_definition VARCHAR(500)
)
BEGIN
    DECLARE v_count INT DEFAULT 0;
    SELECT COUNT(*) INTO v_count
    FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE()
      AND TABLE_NAME = p_table_name
      AND COLUMN_NAME = p_column_name;
    IF v_count = 0 THEN
        SET @sql = CONCAT('ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ', p_column_definition);
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END //
DELIMITER ;

CALL proc_add_column_if_not_exists('erp_mp_mbom', 'issue_date', "datetime DEFAULT NULL COMMENT '下发时间'");

DROP PROCEDURE IF EXISTS `proc_add_column_if_not_exists`;
