Brandon t
Brandon t

Reputation: 33

SQL Server merge with dates

I currently have a table with a column named Date and it's default values is getdate(). I am using a merge statement and I only want it to update if the file is uploaded on the same date. If it's a different date I want to insert a new row. my question is, will this work:

var sqlCommand = string.Format(@"
  MERGE [HEWreport] AS target USING (
    select @Property_ID as Property_ID, @val as {0}
  ) AS source ON (
    target.Date = getdate()
  ) WHEN MATCHED THEN UPDATE SET {0}= source.{0} 
    WHEN NOT MATCHED THEN INSERT (Property_ID, {0}) 
    VALUES (source.Property_ID, source.{0});", column);

Upvotes: 0

Views: 1333

Answers (1)

Joe C
Joe C

Reputation: 3993

It is hard to validate the syntax without sample data. I do believe that will work for you but you want to match on more than date, I am guessing property_id?

Currently your query will update all records with todays date.

EDIT: Use Convert(Date, target.Date) = Convert(Date, getdate())

Upvotes: 1

Related Questions