Monday, February 9, 2015

A sproc I wrote today for taking the first four unique entries out of the password history for a user.

CREATE PROCEDURE [dbo].[CheckPasswordList]
@Username VARCHAR (50)
AS
DECLARE @Gold varchar(50)
DECLARE @Silver varchar(50)
DECLARE @Bronze varchar(50)
SET @Gold = null
SET @Silver = null
SET @Bronze = null
DECLARE @Placeholder varchar(50)
DECLARE @Table TABLE
(
   Password varchar(50)
)
DECLARE hydrator CURSOR FOR
   SELECT Password FROM PasswordHistory
   WHERE Username = @Username
   ORDER BY DateAdded DESC
OPEN hydrator
FETCH NEXT FROM hydrator into @Placeholder
   WHILE @@FETCH_STATUS = 0
      BEGIN
         IF @Gold is null
            BEGIN
               SET @Gold = @Placeholder
               INSERT INTO @Table (Password)
               VALUES (@Placeholder)
            END
         ELSE
            BEGIN
               IF @Silver is null
                  BEGIN
                     If @Placeholder <> @Gold
                        BEGIN
                           SET @Silver = @Placeholder
                           INSERT INTO @Table (Password)
                           VALUES (@Placeholder)
                        END
                  END
               ELSE
                  BEGIN
                     IF @Bronze is null
                        BEGIN
                           If @Placeholder <> @Gold
                              BEGIN
                                 If @Placeholder <> @Silver
                                    BEGIN
                                       SET @Bronze = @Placeholder
                                       INSERT INTO @Table (Password)
                                       VALUES (@Placeholder)
                                    END
                              END
                        END
                     ELSE
                        BEGIN
                           If @Placeholder <> @Gold
                              BEGIN
                                 If @Placeholder <> @Silver
                                    BEGIN
                                       If @Placeholder <> @Bronze
                                          BEGIN
                                             INSERT INTO @Table (Password)
                                             VALUES (@Placeholder)
                                             BREAK
                                          END
                                    END
                              END
                        END
                  END
            END
         FETCH NEXT FROM hydrator into @Placeholder
      END
CLOSE hydrator
DEALLOCATE hydrator
SELECT * FROM @Table
RETURN

No comments:

Post a Comment