SQL Script to generate script for existing database permissions

-***********************************************************************************/ -- Description : SQL Script to generate script for existing database permissions -This Script will generate a Script, which can used to copy the permissions from one server to another Server -- Compatibility : 7.0+ -- This Script should be executed in specific database or master -***********************************************************************************/ set nocount on go use Northwind go create table perms ( perms varchar (100) ) declare @name varchar(50) declare @object_id int declare @action tinyint declare @protect_type tinyint declare perms cursor for select su.name, sp.id, sp.action, sp.protecttype from sysusers su inner join sysprotects sp on (su.uid = sp.uid) order by su.name open perms fetch next from perms into @name, @object_id, @action, @protect_type while (@@fetch_status = 0) begin if @action = 26 insert into perms select 'GRANT REFERENCES' + ' ON [' + object_name(@object_id) + '] TO if @action = 178 insert into perms select 'GRANT CREATE FUNCTION TO ' + @name if @action = 193 insert into perms select 'GRANT SELECT' + ' ON [' + object_name(@object_id) + '] TO ' + if @action = 195 insert into perms select 'GRANT INSERT' + ' ON [' + object_name(@object_id) + '] TO ' + if @action = 196 insert into perms select 'GRANT DELETE' + ' ON [' + object_name(@object_id) + '] TO ' + if @action = 197 insert into perms select 'GRANT UPDATE' + ' ON [' + object_name(@object_id) + '] TO ' + if @action = 198 insert into perms

' + @name

@name

@name

@name

@name

3. I shared some screen shots. 5. What are the permissions are being assigned to a that login ? What all this user can do to at individual database object level ? What server permissions are being assigned to this user? What all tables can be modified by a particular user ? What all stored procedures can be executed by this user ? any many more…. . 4. 6. @object_id.select 'GRANT CREATE TABLE TO ' + @name if @action = 203 insert into perms select 'GRANT CREATE DATABASE TO ' + @name if @action = 207 insert into perms select 'GRANT CREATE VIEW TO ' + @name if @action = 222 insert into perms select 'GRANT CREATE PROCEDURE TO ' + @name if @action = 224 insert into perms select 'GRANT EXECUTE' + ' ON [' + object_name(@object_id) + '] TO ' + @name if @action = 228 insert into perms select 'GRANT BACKUP DATABASE TO ' + @name if @action = 233 insert into perms select 'GRANT CREATE DEFAULT TO ' + @name if @action = 235 insert into perms select 'GRANT BACKUP LOG TO ' + @name if @action = 236 insert into perms select 'GRANT CREATE RULE TO ' + @name insert into perms values ('GO') fetch next from perms into @name. but he was not pleased with those and he is looking for detailed level report for that particular user. @protect_type end close perms deallocate perms select * from perms drop table perms set nocount off What all Database Permissions are being assigned to a particular login ? During this season. Auditor was very tuff and he identified a suspected login and he wanted to know 1. 2. Graphically. @action.

b.PermissionName COLLATE Latin1_General_100_CI_AI FROM ##ObjectLevel o DROP TABLE ##ObjectLevel REVERT The above script will. This script will show exactly what permissions a login/user has. at all levels of SQL Server. 'SERVER') a UNION ALL SELECT NULL.name COLLATE Latin1_General_100_CI_AI. o.subentity_name. t.permission_name COLLATE Latin1_General_100_CI_AI AS 'Permission Name' FROM fn_my_permissions(NULL.databases d CROSS APPLY fn_my_permissions(QUOTENAME(d.objects t CROSS APPLY fn_my_permissions(QUOTENAME(t.name.Name COLLATE Latin1_General_100_CI_AI. a.subentity_name COLLATE Latin1_General_100_CI_AI AS 'Subentity Name'.permission_name COLLATE Latin1_General_100_CI_AI FROM sys.SubentityName COLLATE Latin1_General_100_CI_AI. ''OBJECT'') c' SELECT NULL AS 'Database Owning Object'. o. NVARCHAR(128) FROM sys. NVARCHAR(128). which was being written by Pete Carter. NVARCHAR(128). EXECUTE AS LOGIN = 'INSERT LOGIN NAME HERE' CREATE TABLE ##ObjectLevel ( DatabaseName Name SubEntityName PermissionName ) EXECUTE sp_msforeachdb 'USE [?] INSERT INTO ##ObjectLevel SELECT db_name(). o.subentity_name COLLATE Latin1_General_100_CI_AI. give you very detailed information at every object of every individual database and might take some (30 sec) time to execute too. b. . c. @@SERVERNAME AS 'Securable Name'. a.name).permission_name NVARCHAR(128). c. right the way through to object-level permissions in every database.DatabaseName COLLATE Latin1_General_100_CI_AI.To answer all these questions. d. I used a script. 'DATABASE') b UNION ALL SELECT o.name). from permissions at the instance level.

IndexSizeKB int NULL. Following script will Returns name. IndexCount int NULL. check output as image /* Returns name. type. Number of records in those tables 3.Note : In case. rows. TableName sysname NULL. Unused varchar(256) NULL) CREATE TABLE #DBTables (Instance sysname NULL. Does all tables have indexes or not 4. SQL Script to find find database object information 1. rows. How much space is being occupied by a particular table 6. Rows int. DataSizeKB int NULL. TableRows int NULL. UnusedKB int NULL) DECLARE DECLARE DECLARE DECLARE DECLARE DECLARE DECLARE @DatabaseName varchar(64) @TableName varchar(256) @xtype char(2) @TableRows int @IndexCount int @ReservedKB int @DataSizeKB int . TableType char(2). number of indexes and other key information about the database SQL2000 and later . How many indexes are built on a particular table 5. Index_Size varchar(256) NULL. you want to get result for some specific database then simply add WHERE d. DBName sysname NULL. number of indexes and other key information about the database SQL2000 and later */ SET NOCOUNT ON CREATE TABLE #TableInfo (Name sysname NULL. what all tables exists on database 2.name = ‘MyDatabase’ to the end of the second select statement. Reserved varchar(256) NULL. the following script is for you. What is the total index size of a particular table If you are looking for answers to all these question. Data varchar(256) NULL. type.. ReservedKB int NULL.

@ReservedKB = CAST(SUBSTRING(Reserved. @xtype WHILE (@@FETCH_STATUS = 0) BEGIN TRUNCATE TABLE #TableInfo IF @xtype = 'U' INSERT INTO #TableInfo exec sp_spaceused @TableName . @updateusage = 'TRUE' ELSE INSERT INTO #TableInfo exec sp_spaceused @TableName SELECT @TableRows = Rows. @TableName..[' + so. 1)-1) AS int). 1. 1)-1) AS int) FROM #TableInfo SELECT @IndexCount = COUNT(*) FROM sysindexes WHERE id=OBJECT_ID(@TableName) AND name NOT LIKE '_WA_Sys%' AND indid > 0 INSERT INTO #DBTables VALUES (@@SERVERNAME. @DataSizeKB. @IndexSizeKB = CAST(SUBSTRING(Index_Size. Unused.xtype FROM sysobjects so INNER JOIN sysusers su ON (so. CHARINDEX('KB'. CHARINDEX('KB'.uid) WHERE so.xtype in ('U'.sysprocesses WHERE spid=@@SPID OPEN cs FETCH NEXT FROM cs INTO @TableName. @DatabaseName. @TableRows. @DataSizeKB = CAST(SUBSTRING(Data. @xtype. CHARINDEX('KB'. Index_Size. Reserved. @IndexSizeKB. 1.name + ']'.uid = su.name + '. @UnusedKB = CAST(SUBSTRING(Unused. 'S') SELECT @DatabaseName = DB_NAME(dbid) FROM master. 1)-1) AS int). Data. @IndexCount. so.DECLARE @IndexSizeKB int DECLARE @UnusedKB int DECLARE cs CURSOR FOR SELECT su. @UnusedKB) . 1. @ReservedKB. 1)-1) AS int). 1. CHARINDEX('KB'.

DBName. ReservedKB. TableName. TableRows. @xtype END CLOSE cs DEALLOCATE cs SELECT Instance. IndexCount. UnusedKB FROM #DBTables DROP TABLE #DBTables DROP TABLE #TableInfo . IndexSizeKB.FETCH NEXT FROM cs INTO @TableName. DataSizeKB. TableType.