-- 1/3 : Création de la table temporaire
DROP TABLE IF EXISTS w_dbsetup
;

CREATE TEMP TABLE w_dbsetup AS
    SELECT
    CASE WHEN t_dashboard_onglet.oid = 0 THEN t_dashboard_onglet.oid ELSE 1 END AS oid,
    t_dashboard.code as dashboard_code,
    name,
    index
		FROM pmsi.t_dashboard_onglet
		LEFT JOIN pmsi.t_dashboard ON t_dashboard.oid = dashboard_id
		ORDER BY 2, 4 LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    dashboard_code,
    name,
    index
)
VALUES
(1, 'CTI_CAS', 'CAS', 1)
;

-- 3/3 : Màj de la table iCTI
TRUNCATE pmsi.t_dashboard_onglet;

INSERT INTO pmsi.t_dashboard_onglet(dashboard_id, name, index)
	SELECT
		t_dashboard.oid,
		w_dbsetup.name,
		w_dbsetup.index
	FROM w_dbsetup
	LEFT JOIN pmsi.t_dashboard ON t_dashboard.code = dashboard_code
;