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
Was it helpful?

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top