Consider this table:
pk uniqueidentifier
fk bigint
clock datetime
I have an SP that will update a record if there is a record with the same date value and insert otherwise.
SET @id =(SELECT TOP(1) [pk] FROM [MyTable] WHERE [fk] = @fk AND YEAR(clock) = YEAR(@clock) AND MONTH(clock) = MONTH(@clock) AND DAY(clock) = DAY(@clock) ORDER BY clock DESC)
IF (@id IS NOT NULL) BEGIN
// Update
END
ELSE BEGIN
// Insert
END
Now I want to change this SP up a little.
If there are zero records for that day, I want to insert a record.
If there is one record for that day, I want to insert a record.
If there are two records for that day, I want to update the record with the greatest CLOCK value.
Please help me find a slick way of doing this.
Thanks,
Einar
Semper ubi sub ubi.