Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Find a record
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Versions des environnements
SQL Server:
SQL Server 2005
Divers
Thread ID:
01287531
Message ID:
01287570
Vues:
12
I used ideas from both Borislav's and Naomi's post.
This is the code that I think will work for me. I will have to test it better to be sure.

@fk and @clock are passed into the SP, and are int and datetime respectively.
SELECT	@Count = COUNT(*),
	@MinTime = MIN([Clock]),
	@MaxTime = Max([Clock]) 
FROM	[MyTable]  
WHERE	[Fk] = @fk AND
	YEAR([Clock]) = YEAR(@clock) AND
	MONTH([Clock]) = MONTH(@clock) AND
	DAY([Clock]) = DAY(@clock) 

IF @Count < 2 BEGIN
	PRINT 'INSERT'
END ELSE BEGIN
	DECLARE @PkToUpdate AS uniqueidentifier

	IF (@clock BETWEEN @MinTime AND @MaxTime) BEGIN
		PRINT 'DO NOT DO ANYTHING BECAUSE IT IS BETWEEN'
	END ELSE IF (@clock < @MinTime) BEGIN
		PRINT 'UPDATE row where Clock is @MinTime'
		SET @PkToUpdate = (	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] ASC)
	END ELSE BEGIN
		PRINT 'UPDATE row where Clock is @MaxTime'
		SET @PkToUpdate = (	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)
	END

	IF (@PkToUpdate IS NOT NULL) BEGIN
		PRINT 'Update!!!!!'
		PRINT @PkToUpdate
	END
END
Is there a way to make this faster, better or prettier?
Semper ubi sub ubi.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform