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

CREATE TEMP TABLE w_dbsetup AS
    SELECT       oid,      code,      texte,      is_cti,       is_essentiel     FROM compta.t_rapports      WHERE is_cti = '1'     ORDER BY code      LIMIT 0
;

-- 2/3 : Peuplement de la table temporaire
INSERT INTO w_dbsetup(
    oid,
    code,
    texte,
    is_cti,
    is_essentiel
)
VALUES
(1, 'CTI_BAL1', 'X EXEMPLE CTI - Balance', '1', '0'),
(1, 'CTI_ESS_1', 'COMPTA Essentiels 1', '1', '1'),
(1, 'CTI_ESS_2', 'COMPTA Essentiels 2', '1', '1'),
(1, 'CTI_PF_AGG', 'CTI Portfolio', '1', '1'),
(1, 'CTI_PORTFO', 'CTI Portfolio ', '1', '1')
;

-- 3/3 : Màj de la table iCTI
UPDATE compta.t_rapports SET         texte = w_dbsetup.texte,         is_cti = w_dbsetup.is_cti,         is_essentiel = w_dbsetup.is_essentiel        FROM w_dbsetup       WHERE t_rapports.code = w_dbsetup.code;        INSERT INTO compta.t_rapports(oid, code, texte, is_cti, is_essentiel)        SELECT oid, code, texte, is_cti, is_essentiel FROM w_dbsetup WHERE oid = 0 AND oid NOT IN (SELECT oid FROM compta.t_rapports);              INSERT INTO compta.t_rapports(code, texte, is_cti, is_essentiel)        SELECT code, texte, is_cti, is_essentiel FROM w_dbsetup WHERE oid <> 0 AND code NOT IN (SELECT code FROM compta.t_rapports);