SQL Server Database Permission Scripts

#1 - Script to get list of all database name and its user in SQL Server

  1. USE MASTER  
  2. GO  
  3.   
  4. SELECT SL.dbname AS 'Database Name',SL.name AS 'User Name',SP.type_desc AS 'Login Type'
  5. SL.denylogin, SL.hasaccess, SL.isntname, SL.isntname, SL.isntgroup, SL.isntuser, SL.sysadmin,  
  6. SL.securityadmin, SL.serveradmin, SL.setupadmin, SL.processadmin, SL.diskadmin, SL.diskadmin,  
  7. SL.dbcreator, SL.bulkadmin FROM sys.server_principals AS SP  
  8. INNER JOIN sys.syslogins AS SL ON SP.SID = SL.SID  

#2 - Script to get list of all database users and their roles in SQL Server

  1. Use Master  
  2. GO  
  3.   
  4. exec sp_msForEachDb ' use [?]  
  5. select db_name() as [Databast Name], r.[nameas [Role], p.[nameas [Member Name],  
  6. p.[default_schema_name] as [Schema],p.[principal_id] as [Principal Id]  
  7. from  
  8. sys.database_role_members m  
  9. join  
  10. sys.database_principals r on m.role_principal_id = r.principal_id  
  11. join  
  12. sys.database_principals p on m.member_principal_id = p.principal_id'  

#3 - Script to get list of users and their permission with all stored procedure in SQL Server database

  1. use <<databasename>>  
  2. GO  
  3.   
  4. select sys.schemas.name 'Schema'  
  5. , sys.objects.name 'Stored Procedure'  
  6. , sys.database_principals.name username  
  7. , sys.database_permissions.type permissions_type  
  8. , sys.database_permissions.permission_name  
  9. , sys.database_permissions.state permission_state  
  10. , sys.database_permissions.state_desc  
  11. , state_desc + ' ' + permission_name + ' on ['+ sys.schemas.name + '].[' + sys.objects.name + '] to [' + sys.database_principals.name + ']' COLLATE LATIN1_General_CI_AS  
  12. from sys.database_permissions  
  13. join sys.objects on sys.database_permissions.major_id = sys.objects.object_id  
  14. join sys.schemas on sys.objects.schema_id = sys.schemas.schema_id  
  15. join sys.database_principals on sys.database_permissions.grantee_principal_id = sys.database_principals.principal_id  
  16. Where sys.objects.type IN ('P')  
  17. order by 1, 2, 3, 5  

Tested in SQL server 2008 R2, 2008, 2012.

Comments

Popular Posts

Contact Application Using ASP.NET Core Web API, Angular 6.0, And Visual Studio Code - Part One

Contact Application Using ASP.NET Core Web API, Angular 6.0, And Visual Studio Code - Part Two

Send Email With SharePoint Lookup Columns Data Using Power Automate

Contact application - Upgrade Asp.net Core 2.0 to 2.1

Create SSRS Drill Down Report

Get group permission list in which current user is member using JavaScript in SharePoint Online CSOM

Typescript Basics

Chat Application using Angular 8, Asp.net Core 2.2.0, Signal R 1.1.0