2016-12-15 44 views
1

我有點卡住我應該如何處理我的存儲過程。這個SP的想法是獲取這些傳入值,並將它們與我的User_Passwords表進行比較。根據公司政策查看該表是否太長。刪除舊的將表縮小到正確的大小。然後將傳入的密碼與先前的密碼進行比較,以在我的VB.Net代碼上拋出錯誤,指出他們需要選擇不同的密碼,因爲之前使用過密碼。我對我難過的地方發表了評論。存儲過程,需要從表中刪除,然後比較傳入值

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 tried 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 

後來在SP,我將加入到表,如果密碼是新的密碼。

+0

請記住,這些值在那裏進行測試,他們實際上從我的VB代碼 –

+5

我的第一個問題是你爲什麼要採用明文存儲的密碼來嗎?我意識到這實際上並不是問題的一部分,但不存儲鹹味和散列的密碼是非常糟糕的。許多人只有2-3個密碼供他們使用。 –

+0

如果@CurrentPassword = User_Passwords.up_Password表示string = table.ColumnName。除非您正在迭代記錄,否則這是無效的語法。 – RSSM

回答

3

@SeanLange提高非常好的點。我會建議你跟上這一點。

IF @CurrentPassword = User_Passwords.up_Password不是一個有效的陳述。 Exists允許您檢查查詢是否返回任何結果。

下面是一個例子:

IF EXISTS (SELECT 1 FROM User_Passwords WHERE up_Password = @CurrentPassword) 
    BEGIN 

     PRINT 'We got one' 
    END 
ELSE 
    BEGIN 

     PRINT 'No match' 
    END 
+0

非常感謝! –

相關問題