How to create logins and users in the Azure SQL Database

It was easy to create the logins and users in the SQL Server using the SSMS GUI. When we are working with the Azure SQL Server it is not allowed to create the logins and users using the GUI.

Here, I am creating the logins and user account using the TSQL Script.

—step1: Createlogin in the Master Database—-
create login sam1 with password=’password@789′

–Step2:Create User in the Master database—–
create user sam1 from login sam1

–Step3:Create user in the user database——-
create user sam1 from login sam1

–Step4: Adding the user to the Role—
EXEC sp_addrolemember ‘db_owner’, ‘sam1’;

Last, we can check the user accounts permissions using the following script.

SELECT DP1.name AS DatabaseRoleName,
isnull (DP2.name, ‘No members’) AS DatabaseUserName
FROM sys.database_role_members AS DRM
RIGHT OUTER JOIN sys.database_principals AS DP1
ON DRM.role_principal_id = DP1.principal_id
LEFT OUTER JOIN sys.database_principals AS DP2
ON DRM.member_principal_id = DP2.principal_id
WHERE DP1.type = ‘R’
ORDER BY DP1.name;

https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-principals-transact-sql?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-database-role-members-transact-sql?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-role-transact-sql?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-addrolemember-transact-sql?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/relational-databases/security/authentication-access/database-level-roles?view=sql-server-ver15

Published by Santhosh Reddy

I am working as a SQL Server DBA.

One thought on “How to create logins and users in the Azure SQL Database

Leave a comment