如何在select中插入

时间:2014-07-07 14:59:28

标签: postgresql

我想在选择中使用insert into:

INSERT INTO action_periodique (action_periodique_valeur_id, affaire_id , typetache_id , etattache_id , actionemail_id)
SELECT (
    INSERT INTO action_periodique_valeur (nom , debut_rappel , fin_rappel , freq_rappel , champ_id , affaire_id)
    SELECT nom , debut_rappel , fin_rappel , freq_rappel , champ_id , 430
    FROM action_periodique_valeur
    WHERE action_periodique.action_periodique_valeur_id = action_periodique_valeur.id
    RETURNING id ), 
430 , typetache_id , etattache_id , 
    (INSERT INTO actionemail ( copiecarbone , destinataires , expediteur , message , repondrea , sujet)
     SELECT copiecarbone , destinataires , expediteur , message , repondrea , sujet
     FROM actionemail
     WHERE action_periodique.actionemail_id = actionemail.id )
FROM action_periodique
where affaire_id = 170

有可能吗?

1 个答案:

答案 0 :(得分:0)

您几乎没有提供有关您要执行的操作的信息。

对我来说似乎是你正在寻找修改CTE的数据。

这样的事情:

with c1 as (
    INSERT INTO action_periodique_valeur (nom , debut_rappel , fin_rappel , freq_rappel , champ_id , affaire_id)
    SELECT nom , debut_rappel , fin_rappel , freq_rappel , champ_id , 430
    FROM action_periodique_valeur
    WHERE action_periodique.action_periodique_valeur_id = action_periodique_valeur.id
    RETURNING id
, c2 as  (
  INSERT INTO actionemail ( copiecarbone , destinataires , expediteur , message , repondrea , sujet)
  SELECT copiecarbone , destinataires , expediteur , message , repondrea , sujet
  FROM actionemail
  WHERE action_periodique.actionemail_id = actionemail.id
  returning id
)
INSERT INTO action_periodique (action_periodique_valeur_id, affaire_id , typetache_id , etattache_id , actionemail_id)
SELECT (select id from c1),
       430, 
       typetache_id, 
       etattache_id , 
       (select id from c2)
FROM action_periodique
where affaire_id = 170;

如果这不是你想要的,你需要方式更具体。