0

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

rickyalbert
  • 2,552
  • 4
  • 21
  • 31
  • possible duplicate of [IF() statement alternative in SQLite](http://stackoverflow.com/questions/4874285/if-statement-alternative-in-sqlite) – andrewgu Aug 03 '15 at 20:06
  • have you considered using transaction and performing the logic in the application layer? – Leonid Usov Aug 03 '15 at 20:10

1 Answers1

1

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

Leonid Usov
  • 1,508
  • 12
  • 16