Question

The following works quite well:

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)

I'd like to use this select as subselect in my insert:

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))

But this statement isn't working.

CUSTOMER has the following structure:

  • KNR (customer number) as varchar <-- primary key
  • FIRSTNAME as varchar
  • LASTNAME as varchar
Était-ce utile?

La solution

You do not need to use VALUES statement in this case. Try this

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top