Question

I am trying to create a stored procedure in SQL server 2012 that will update the password for the username given along with the new password. I am getting an error "Incorrect syntax near the keyword 'WHERE'". Any ideas how to fix this? Thanks!

CREATE PROCEDURE [dbo].[ChangePassword] (@User_ID varchar(30), @Password varchar(20)) AS
INSERT INTO [User](Password)
VALUES (@Password)
WHERE User_ID = (@User_ID)
Was it helpful?

Solution

You are trying to Insert a new record, For updating an existing record you need to use UPDATE command see below.

CREATE PROCEDURE [dbo].[ChangePassword] 
@User_ID varchar(30), 
@Password varchar(20) 
AS
BEGIN
  SET NOCOUNT ON;

    UPDATE [User]
    SET [Password]  = @Password
    WHERE [USER_ID] = @User_ID
END

Also avoid using sql-server key words as your column names, if you do have some columns like that make sure you use square brackets [] around them when using inside a query.

OTHER TIPS

syntax is incorrect. If you want to update a table try this:

CREATE PROCEDURE [dbo].[ChangePassword] 
@User_ID varchar(30), 
@Password varchar(20)
AS
UPDATE  [User]
SET Password = @Password
WHERE   User_ID = (@User_ID)

If you want to insert a new user try this:

INSERT INTO [User] (User_ID, Password)
SELECT @User_ID, @Password
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top