Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Help modifying my SP
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Help modifying my SP
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01287482
Message ID:
01287482
Views:
56
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.
Next
Reply
Map
View

Click here to load this message in the networking platform