I’m using SQL Server 2008. I’m looking for a creative way to save and update a list of dates in our database.
I’m going to collect a list of dates from the application and I will need to check if each value already exists, if not add, and then delete any dates not in the list that are already stored in the database.
The easiest thing I can think of is to delete all dates associated to this particular request and then iterate over each item in my list and insert into database.
Does anyone have a more elegant idea?
You can use merge. You can also load the dates into a temporary table and do an insert such as:
The toinsert alias uses a left outer join to do a “not in”. I often find that this works better. Regardless of how you set up the queries (like this or with a merge), you should put in an index on the dates. It should make things go faster.