Introduction
While working with Azure SQL using SQL server management studio (SSMS), most of the features which we used to get through SSMS GUI while connecting with OnPre/IaaS SQL-Server are not available (e.g.: get the user's role assignment, create user/role ..)
Here are a few useful T-SQL queries.
To create a role:
First, check if we have the given role existing in the database or not. If it doesn't exist, then let's create it.
- IF DATABASE_PRINCIPAL_ID('<<your-role-name>>') IS NULL
- BEGIN
- -- Add Role here
- CREATE ROLE [<<your-role-name>>];
- END
To create a user:
First, check if the given user is present in the database or not. If not then let's create the user.
- -- Create User if not exists [<<your-user-name>>]
- IF NOT EXISTS (SELECT [name]
- FROM
- sys.database_principals
- WHERE [name] = '<<your-user-name>>' )
- BEGIN
- CREATE USER [<<your-user-name>>] FROM EXTERNAL PROVIDER ;
- END
Get user, its description & grant permission summary
- SELECT DISTINCT pr.principal_id, pr.name, pr.type_desc,
- pr.authentication_type_desc, pe.state_desc, pe.permission_name
- FROM sys.database_principals AS pr
- JOIN sys.database_permissions AS pe
- ON pe.grantee_principal_id = pr.principal_id;
Join the conversation! Your thoughts help the community grow.