How do you use the ALTER LOGIN statement to change the properties of an existing login?
Posted by LeoRobs
Last Updated: June 17, 2024
The ALTER LOGIN statement in SQL Server is used to modify the properties of an existing login. This includes options such as changing the password, enabling or disabling the login, modifying the default database, and more. Here’s the basic syntax for the ALTER LOGIN statement:
ALTER LOGIN login_name 
    [ WITH option1 [, option2, ... ] ]
Common Options for ALTER LOGIN
1. CHANGE PASSWORD: Change the password for the login. 2. DISABLE: Disable the login. 3. ENABLE: Enable a previously disabled login. 4. DEFAULT_DATABASE: Set or change the default database for the login. 5. DEFAULT_LANGUAGE: Set or change the default language for the login. 6. CHECK_EXPIRATION: Enable or disable password expiration. 7. CHECK_POLICY: Enable or disable password policy enforcement.
Examples
1. Change Password:
ALTER LOGIN myLogin WITH PASSWORD = 'NewPassword123!';
2. Enable a Disabled Login:
ALTER LOGIN myLogin ENABLE;
3. Disable a Login:
ALTER LOGIN myLogin DISABLE;
4. Change Default Database:
ALTER LOGIN myLogin WITH DEFAULT_DATABASE = MyDatabase;
5. Set Default Language:
ALTER LOGIN myLogin WITH DEFAULT_LANGUAGE = French;
6. Enable Password Expiration and Policy:
ALTER LOGIN myLogin WITH CHECK_EXPIRATION = ON, CHECK_POLICY = ON;
Important Notes
- You need appropriate permissions to execute the ALTER LOGIN statement. Generally, you would need to be a member of the sysadmin role or have the ALTER ANY LOGIN permission. - Be careful when changing passwords, especially in production environments, as it may disrupt any applications using that login. - Ensure that the chosen password meets the complexity requirements defined in the server's security policy. By using the ALTER LOGIN statement properly, you can effectively manage the security and configuration of your SQL Server logins.