This article demonstrates how to use T-SQL to change the password for a SQL Server login.
You can change the password for another login (assuming you have the appropriate permissions), or you can change it for your own login. This article outlines both of these scenarios.
Example
To change the password for a SQL Server login, use the ALTER LOGIN
statement with the WITH PASSWORD
argument.
Here’s an example.
ALTER LOGIN Bart
WITH PASSWORD = '$trongPwd123';
You can run that statement to change the password of your own login or another login, however, it does require that you have ALTER ANY LOGIN
permission.
If you don’t have ALTER ANY LOGIN
permission, you’ll get this error:
Msg 15151, Level 16, State 1, Line 2 Cannot alter the login 'Bart', because it does not exist or you do not have permission.
Change Your Own Password
You can use the statement from the previous example to change your own login’s password (i.e. when you’re logged in as the login). As mentioned, you’ll need ALTER ANY LOGIN
permission.
However, you can still change your own password even if you don’t have ALTER ANY LOGIN
permission.
The way to do this is by including your old password as well as the new password.
ALTER LOGIN Bart
WITH PASSWORD = 'StrongPwd!'
OLD_PASSWORD = '$trongPwd123';
I actually logged in as Bart and ran that statement and received the following confirmation.
Commands completed successfully.
Bart doesn’t actually have ALTER ANY LOGIN
permission, but he was still able to change his own password by providing his old password.
Here’s what happens if Bart tries to change the password without supplying the old password.
ALTER LOGIN Bart
WITH PASSWORD = 'WheelyStwongPwd!';
Result:
Msg 15151, Level 16, State 1, Line 1 Cannot alter the login 'Bart', because it does not exist or you do not have permission.
But as you might expect, if I log in as sa
and run it again, I’m able to change it without providing the old password.
ALTER LOGIN Bart
WITH PASSWORD = 'WheelyStwongPwd!';
Result:
Commands completed successfully.