0

How do I add condition into the BATCH insert. I am using statement like following to insert data into the table.

INSERT INTO table   (id, date1,date2 ) SELECT ?, ?, ?  WHERE NOT EXISTS (SELECT 1 FROM table WHERE id= ? and COALESCE (date1::timestamp = ?))

Above INSERT will add entry into all the columns always. Is there any way I can have conditional entry into the table. For example I want to insert only date2 if date1 and id is already present. Goal is to not update the date1 if it is already present.

1 Answer 1

1

Here's my understanding of your question: you want to update existing rows in 'table' where 'table' has values for columns 'id' and 'date1'. The update you want to make is to put a date in the column 'date2'.

This will add the current date to 'date2' wherever 'id' and 'date1' are already filled out:

UPDATE table 
SET date2 = getdate()
WHERE id IS NOT NULL 
    AND date1 IS NOT NULL 

Here you can set date2 to anything you want. If you need a date from a different table, you can get it by changing only that line to

SET date2 = (SELECT datecolumn FROM othertable WHERE ...)
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.