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
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01287482
Message ID:
01287524
Views:
11
Thanks Sergey. A quick and awesome solution as always. The only problem is that the requirements have changed. I'll post a new post.

>Try
>
>SELECT @id = [pk] FROM [MyTable]
> 	WHERE [fk] = @fk AND YEAR(clock) = YEAR(@clock)
>		AND	MONTH(clock) = MONTH(@clock)
>		AND DAY(clock) = DAY(@clock)
>	ORDER BY clock
>
>IF (@id IS NOT NULL)  AND @@ROWCOUNT > 1 BEGIN
>	// Update
>END
>ELSE BEGIN
>	// INSERT
>END
>
>
>>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.
Previous
Reply
Map
View

Click here to load this message in the networking platform