rickyalbert
rickyalbert

Reputation: 2652

Insert with if condition

I need to create a sql statement that does this work: let's say I have two tables A and B containing integer fields. What I have to achieve is:

if (!(C is contained into A)) insert C into B

I'm using SQLite. Thanks for help

Upvotes: 0

Views: 48

Answers (1)

Leonid Usov
Leonid Usov

Reputation: 1598

Actually, in your specific case it may happen to be as easy as

insert into B (c_value) select c_value from A where c_value = @your_c_value_here

see INSERT statement


sorry I haven't noticed the negation in your question for the C in A condition I have another option for you

with temp_val as (select @your_val_goes_here as val)
insert into b 
select val from temp_val where not exists
(select 1 from a where c = val)

check out this fiddle

Upvotes: 1

Related Questions