Azure SQL - Useful Queries

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. 
  1. IF DATABASE_PRINCIPAL_ID('<<your-role-name>>'IS NULL  
  2. BEGIN  
  3.   -- Add Role here  
  4.   CREATE ROLE [<<your-role-name>>];  
  5. 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.
  1. -- Create User if not exists [<<your-user-name>>]  
  2. IF NOT EXISTS (SELECT  [name]     
  3.             FROM      
  4.             sys.database_principals      
  5.             WHERE   [name] = '<<your-user-name>>'   )  
  6. BEGIN  
  7.    CREATE USER [<<your-user-name>>] FROM  EXTERNAL PROVIDER ;  
  8. END  
Get user, its description & grant permission summary
  1. SELECT DISTINCT pr.principal_id, pr.name, pr.type_desc,     
  2.     pr.authentication_type_desc, pe.state_desc, pe.permission_name    
  3. FROM sys.database_principals AS pr    
  4. JOIN sys.database_permissions AS pe    
  5.     ON pe.grantee_principal_id = pr.principal_id;    
To get the role detail
  1. SELECT     
  2.     drs.role_principal_id  
  3.     , drs.member_principal_id  
  4.     , dp_role.name as  "role_name"  
  5.     , dp_role.type_desc as "role_type_desc"  
  6.     , dp_role.type as "role_type"  
  7.     , dp_member.name  as "member_name"  
  8.     , dp_member.type as "member_type"  
  9.     , dp_member.type_desc as "member_type_desc"  
  10.     , dp_member.authentication_type as "member_authentication_type"  
  11.     , dp_member.authentication_type_desc as "member_authentication_type_desc"  
  12. FROM sys.database_role_members as drs              
  13.     LEFT JOIN sys.database_principals as dp_role    
  14. ON drs.role_principal_id = dp_role.principal_id    
  15.     LEFT JOIN sys.database_principals as dp_member    
  16. ON drs.member_principal_id = dp_member.principal_id    
  17.     WHERE dp_member.name = N'<<your-user-name>>'    
  18. Order by 1    
To get the referenced or referencing object. Sometimes we need to get all the object lists that are dependent on the given stored procedure or to get the object on which the given stored procedure is dependent.
 
To get the object dependencie frequently, use the stored procedure: "sp_depends"
  1. sp_depends '<your-schema>.<your-sp-name>'  
  2. -- 1st Result: Object on which it depends  
  3. -- 2nd Result: Object which depends on this  
A better way to see the object dependencies is to use dm_sql_referenced_entities and dm_sql_referencing_entities
  1. -- Object on which this sp depends  
  2. SELECT  
  3.         referenced_schema_name,  
  4.         referenced_entity_name,  
  5.         referenced_minor_name,  
  6.         referenced_minor_id,  
  7.         referenced_class_desc,  
  8.         is_caller_dependent,  
  9.         is_ambiguous  
  10. FROM  
  11.     sys.dm_sql_referenced_entities ('your-schema>.<your-sp-name>',  
  12.         'OBJECT')  
  13.   
  14. -- Object which depends on this sp  
  15. SELECT referencing_schema_name,   
  16.     referencing_entity_name,   
  17.     referencing_id,   
  18.     referencing_class_desc,   
  19.     is_caller_dependent    
  20. FROM sys.dm_sql_referencing_entities ('<your-schema>.<your-sp-name>',   
  21.         'OBJECT');