I'm using Postgres's Common Table Expressions, and would - if possible - to insert data into a table if the values in the data is not null:
WITH prod1 AS (
INSERT INTO p1 ... RETURNING id),
prod2 AS (
INSERT INTO p2 (fk,foo,bar) VALUES (prod1.id,@Foo,@Bar)
RETURNING id)
If the values @Foo and @Bar equate to null, is it possible for postgres to skip the insertion of data into p2 otherwise it'll create a blank reference? Or, do I have to manually check and insert as required via code?
insert into p2 (fk, foo, bar)
select prod1.id, @foo, @bar
from prod1
where @foo is not null or @bar is not null