如何将返回的id存储到其他表中?

时间:2017-05-18 13:15:55

标签: postgresql

我想将表Soggetto返回的id存储到其他2个表中(Tipologia> column soggetto_id和Tipologia2> column soggetto_id)

我在postgresql中有这个CTE。

WITH retid AS (
  INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA")
      VALUES ($1, $2, $3, $4)
      RETURNING id
 ),
 t as (
  INSERT INTO "Tipologia" (privato, azienda)
      VALUES ($5, $6)
 ),
 t2 as (
  INSERT INTO "Tipologia2" (cliente, fornitore)
      VALUES ($7, $8)
 )
SELECT id
FROM retid;

1 个答案:

答案 0 :(得分:0)

你必须在两个引用表中指定“Soggetto”.id的目的地,如下所示:

with i as (
   INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") 
   VALUES($1, $2, $3, $4) RETURNING id
 )
 , e as (
   INSERT INTO "Tipologia"(soggetto_id,privato, azienda) select SELECT id, $5, $6
   FROM i
 )
 INSERT INTO "Tipologia2"(soggetto_id,cliente, fornitore) select SELECT id,$7, $8
   FROM i