0

I want to do bulk insert only if there is no matching email

I tried this, but I don't know how to add a where clause.

INSERT INTO user (
   'pk',
   'email'
)
SELECT UNNEST(ARRAY[10, 11, 12, 13, 14, 15, 16]),
      UNNEST(ARRAY['[email protected]', '[email protected]', .........])
WHERE something.......

1
  • your example should work (with a couple modifications) -- could you share the error message that you see, in addition to the actual query that you're trying to do it with? Commented Oct 11, 2022 at 7:09

1 Answer 1

1

Sample 1:

with tbl(a,b) as (
select
    UNNEST(ARRAY[10, 11, 12]),
    UNNEST(ARRAY['[email protected]', '[email protected]', '[email protected]'])
)
insert into user (pk, email)
select a, b from tbl 
left join user on tbl.b = user.email 
where user.email is null 

Sample 2:

with tbl(a,b) as (
select
    UNNEST(ARRAY[10, 11, 12]),
    UNNEST(ARRAY['[email protected]', '[email protected]', '[email protected]'])
)
insert into user (pk, email)
select a, b from tbl 
where not exists (select 1 from user where user.email = tbl.b)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.