DROP PROCEDURE IF EXISTS mocha_set_attribute_value;

CREATE PROCEDURE mocha_set_attribute_value
(
    IN p_source_inst_id INT,
    IN p_attribute_inst_id INT,
    IN p_value TEXT,
    IN p_user_inst_id INT,
    IN p_effective_date DATETIME
)
BEGIN
    DECLARE p_tenant_id INT;
    DECLARE z_effective_date DATETIME;

    SET p_tenant_id = mocha_get_current_tenant();

    IF p_effective_date IS NULL THEN
        SET z_effective_date = NOW();
    ELSE
        SET z_effective_date = p_effective_date;
    END IF;

    INSERT INTO mocha_attributes
    (
        tenant_id,
        src_inst_id,
        att_inst_id,
        att_value,
        usr_inst_id,
        att_effective_date
    )
    VALUES
    (
        p_tenant_id,
        p_source_inst_id,
        p_attribute_inst_id,
        p_value,
        p_user_inst_id,
        z_effective_date
    );

END;
