在insert into语句中不存在的子选择

时间:2014-04-25 07:23:19

标签: sql db2

以下效果很好:

SELECT ‘K’ CONCAT CHAR(PNR), FIRSTNAME, LASTNAME
FROM S654321.PERSON P 
WHERE NOT EXISTS(SELECT * 
                 FROM S654321.CUSTOMER C 
                 WHERE C.FIRSTNAME = P.FIRSTNAME AND C.LASTNAME = P.LASTNAME)

我想在我的插页中使用此选择作为子选择:

INSERT INTO S654321.CUSTOMER
VALUES(SELECT ‘K’ CONCAT CHAR(PNR), FIRSTNAME, LASTNAME
       FROM S654321.PERSON P 
       WHERE NOT EXISTS(SELECT * 
                        FROM S654321.CUSTOMER C 
                        WHERE C.FIRSTNAME = P.FIRSTNAME 
                              AND C.LASTNAME = P.LASTNAME))

但这句话不起作用。

CUSTOMER具有以下结构:

  • KNR(客户编号)为varchar< - 主键
  • FIRSTNAME as varchar
  • LASTNAME as varchar

1 个答案:

答案 0 :(得分:1)

在这种情况下,您不需要使用VALUES语句。试试这个

INSERT INTO S654321.CUSTOMER (KNR, FIRSTNAME, LASTNAME)
SELECT ‘K’ CONCAT CHAR(PNR), FIRSTNAME, LASTNAME
   FROM S654321.PERSON P 
   WHERE NOT EXISTS(SELECT * 
                    FROM S654321.CUSTOMER C 
                    WHERE C.FIRSTNAME = P.FIRSTNAME 
                          AND C.LASTNAME = P.LASTNAME)