Insert 2 select in same insert SQL

Insert 2 select in same insert SQL

我有问题,我想用 2 select 插入新的 table 但我不知道是否可行...

我的代码:

insert INTO attributs_libres_lignes VALUES  ( SELECT LIGN_PKID, "INSERT HERE" from ATTRIBUTS_LIGNES a, "HERE THE NUMBER OF COLUMN OF PKID (1 to 4)", "HERE A DATE");
select COLONNE01 from attributs_lignes
union all select COLONNE02 from attributs_lignes
union all select COLONNE03 from attributs_lignes
union all select COLONNE04 from attributs_lignes

我想在第一部分插入第二部分(select COLONNE01....)而不是“INSERT HERE”

旧数据 TABLE:第 1 列(“PKID1”、“一”、“二”、“三”) 第 2 列(“PKID2”,“一”,“二”,“三”)

新 TABLE :

(PKID1, 一, 1) (PKID2, 一, 1) (PKID1, 二, 2) (PKID2, 二, 2) (PKID1, 三, 3) (PKID2, 三, 3)

-

您可以按如下方式使用 CROSS JOIN:

INSERT INTO ATTRIBUTS_LIBRES_LIGNES
SELECT A.LIGN_PKID,
       C.COL_ATTRIBUTES
  FROM ATTRIBUTS_LIGNES A
 CROSS JOIN (
    SELECT COLONNE01 COL_ATTRIBUTES FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE02 FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE03 FROM ATTRIBUTS_LIGNES UNION ALL
    SELECT COLONNE04 FROM ATTRIBUTS_LIGNES
) C

是不是就这么简单?

insert into attributs_libres_lignes
select lign_pkid, colonne01 from attributs_lignes union all
select lign_pkid, colonne02 from attributs_lignes union all
select lign_pkid, colonne03 from attributs_lignes union all
select lign_pkid, colonne04 from attributs_lignes;

我会推荐 cross apply 逆轴。这比 union 更有效,因为它只扫描 table 一次,而不是每个成员子查询一次。

所以:

insert into attributs_libres_lignes (lign_pkid, colonne)
select a.lign_pkid, v.colonne
from attributs_lignes a
cross apply (
    select a.colonne01 colonnefrom dual
    union all select a.colonne02 from dual
    union all select a.colonne03 from dual
    union all select a.colonne04 from dual
) v

请注意,枚举 insert 语句的目标列是一种很好的做法。我假设目标 table 的列是 lign_pkidcolonne,您可能需要查看一下。