Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Help modifying my SP
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Help modifying my SP
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01287482
Message ID:
01287482
Vues:
57
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.
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform