I am trying to write a procedure to change a user's password but I am having a bit of difficulty finishing it off, I have the general gist of it but having a few syntax errors. This is my procedure;
CREATE PROC ChangePassword
@User_Name NVARCHAR(50),
@OldPassword NVARCHAR(50),
@NewPassword NVARCHAR(50),
@ResponseMessage NVARCHAR(250)='' OUTPUT
AS
BEGIN
SET NOCOUNT ON
IF ((
SELECT PasswordHash
FROM dbo.Users
WHERE dbo.Users.user_name=@User_Name)=HASHBYTES('SHA2_512',@OldPassword + (
SELECT CAST(Salt AS NVARCHAR(50))
FROM dbo.Users
WHERE dbo.Users.user_name = @User_Name)))
THEN
UPDATE dbo.Users SET PasswordHash = HASHBYTES('SHA2_512',@NewPassword + (
SELECT CAST(Salt AS NVARCHAR(50))
FROM dbo.Users
WHERE dbo.Users.user_name = @User_Name))
SET @ResponseMessage = 'Password Changed Successfully'
ELSE
SET @ResponseMessage = 'Old Password did not match'
END
It basically checks if the hash of the old password they entered matches the hash of the actual old password and if it does then update it with a hash of the new password but I am having a bit of difficulty with the IF statement as I am getting compilation errors underneath the THEN and ELSE saying incorrect syntax near 'THEN' and ELSE respectively, could someone provide some guidance to fix this?
IFandELSEwithBEGINandENDblocks if necessary.case?CASEexpression can't be used instead of anIF, it's an expression