Procédure stockée qui doit être supprimée de la table, puis comparer la valeur entrante

Je suis un peu coincé sur la façon dont je devrais procéder avec ma procédure stockée. L'idée de ce SP est de prendre ces valeurs entrantes, les comparer à ma table User_Passwords. Voir si cette table est trop longue basée sur la politique de l'entreprise. Supprimez les anciens pour réduire la taille de la table. Puis comparez le mot de passe entrant avec les passwords précédents pour lancer une erreur sur mon code VB.Net indiquant qu'ils doivent choisir un mot de passe différent parce qu'il a été utilisé auparavant. J'ai fait un commentaire sur où je suis perplexe.

DECLARE @UserNumberOfPasswords INT DECLARE @ua_pk uniqueidentifier = GUID DECLARE @ResetDaysAmount INT = 30 DECLARE @AllowedNumberOfPasswords INT = 10 DECLARE @CurrentPasswordDate DATE = GetDate() DECLARE @CurrentPassword varchar(25) = 'Password' DECLARE @PreviousPassword BIT = 0 SELECT * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC SELECT @UserNumberOfPasswords = COUNT(*) FROM User_Passwords AS up WHERE ua_fk = GUID IF @UserNumberOfPasswords > @AllowedNumberOfPasswords BEGIN WITH T AS (SELECT TOP (@UserNumberOfPasswords - (@AllowedNumberOfPasswords - 1)) * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC) DELETE FROM T; END IF @UserNumberOfPasswords = @AllowedNumberOfPasswords BEGIN WITH T AS (SELECT TOP 1 * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC) DELETE FROM T; END --Where I'm stumped. I have sortinged to use 'up_Password' but it's throwing an error --"the multi-part identifier "User_Passwords.up_Password" could not be bound". --The column's type is varchar(25), just like @CurrentPassword IF @CurrentPassword = User_Passwords.up_Password BEGIN WITH T AS (SELECT * FROM User_Passwords WHERE ua_fk = @ua_pk ORDER BY up_PasswordDate ASC) --I know this isn't completed but I want to change the BadPassword =1 

Plus tard dans le SP, je vais append à la table, si le mot de passe est un nouveau mot de passe.

@SeanLange soulève un très bon point . Je vous recommand de suivre cela.

IF @CurrentPassword = User_Passwords.up_Password n'est pas une instruction valide. Existe vous permet de vérifier si une requête renvoie des résultats.

Voici un exemple:

 IF EXISTS (SELECT 1 FROM User_Passwords WHERE up_Password = @CurrentPassword) BEGIN PRINT 'We got one' END ELSE BEGIN PRINT 'No match' END