Scripting User level Permission – SQL2000

This script gives a out script that can be used to script out the existing user permissions in a database.This is compatible only to SQL 2000.Don’t forget to change the database name in parameter @dbname before using the script.

Script

SET NOCOUNT ON
DECLARE @dbname VARCHAR(30)
DECLARE @sql VARCHAR(4000)
DECLARE @sql1 VARCHAR(4000)
SET @dbname = 'Yourdbnamne'
PRINT 'Granting Role Permissions if Any:'
SET @sql1 ='select ''sp_addrolemember ''+b.name+'', ''+ a.name from '+ @dbname+'..sysusers a,'
+@dbname+'..sysusers b, '+ @dbname+'..sysmembers c,master..sysdatabases s
where a.uid = c.memberuid and c.groupuid = b.uid and s.name like ''' +@dbname +''' and a.name <> ''dbo'''
EXEC(@sql1)
CREATE TABLE #DBUSERS (DBNAME VARCHAR(50),USERNAME VARCHAR(50), MEMBERNAME VARCHAR(50), OBJECTNAME VARCHAR(100), PERMGRANTED VARCHAR(200), PERMDENIED VARCHAR(50),COLUMNPERM VARCHAR(10))
SET @sql ='insert into #DBUSERS (DBNAME,USERNAME,OBJECTNAME,PERMGRANTED,PERMDENIED,COLUMNPERM)
select s.name,a.name,c.name,b.actadd,b.actmod,case when (b.seladd is not null or b.selmod is not null or b.updadd is not null or b.updmod is not null or b.refadd is not null or b.refmod is not null) then ''Y'' else ''N'' end
from '+@dbname+'..sysusers a,'+@dbname+'..syspermissions b,'+@dbname+'..sysobjects c,master..sysdatabases s
where a.uid = b.grantee and b.[id] = c.[id] and b.grantee <> 0 and s.name like '''+@dbname+''''
EXEC(@sql)
PRINT 'Granting Object Permissions if Any:'
SELECT 'grant '+CASE PERMGRANTED WHEN 1 THEN 'SELECT'
WHEN 2 THEN 'UPDATE'
WHEN 3 THEN 'SELECT,UPDATE'
WHEN 4 THEN 'REFERENCES'
WHEN 5 THEN 'SELECT, REFERENCES'
WHEN 6 THEN 'UPDATE,REFERENCES'
WHEN 7 THEN 'SELECT,UPDATE,REFERENCES'
WHEN 8 THEN 'INSERT'
WHEN 9 THEN 'SELECT,INSERT'
WHEN 10 THEN 'UPDATE,INSERT'
WHEN 11 THEN 'SELECT,UPDATE,INSERT'
WHEN 12 THEN 'REFERENCES,INSERT'
WHEN 13 THEN 'SELECT,REFERENCES,INSERT'
WHEN 14 THEN 'UPDATE,REFERENCES,INSERT'
WHEN 15 THEN 'SELECT,UPDATE,REFERENCES,INSERT'
WHEN 16 THEN 'DELETE'
WHEN 17 THEN 'SELECT,DELETE'
WHEN 18 THEN 'UPDATE,DELETE'
WHEN 19 THEN 'SELECT,UPDATE,DELETE'
WHEN 20 THEN 'REFERENCES,DELETE'
WHEN 21 THEN 'SELECT,REFERENCES,DELETE'
WHEN 22 THEN 'UPDATE,REFERENCES,DELETE'
WHEN 23 THEN 'SELECT,UPDATE,REFERENCES,DELETE'
WHEN 24 THEN 'INSERT,DELETE'
WHEN 25 THEN 'SELECT,INSERT,DELETE'
WHEN 26 THEN 'UPDATE,INSERT,DELETE'
WHEN 27 THEN 'SELECT,UPDATE,INSERT,DELETE'
WHEN 28 THEN 'REFERENCES,INSERT,DELETE'
WHEN 29 THEN 'SELECT,REFERENCES,INSERT,DELETE'
WHEN 30 THEN 'REFERENCES,INSERT,DELETE'
WHEN 31 THEN 'SELECT,UPDATE,REFERENCES,INSERT,DELETE'
WHEN 32 THEN 'EXECUTE' ELSE NULL END+' on '+OBJECTNAME+' to '+USERNAME
FROM #DBUSERS WHERE PERMGRANTED <> 0
PRINT 'Revoking Object Permissions if Any:'
SELECT 'revoke '+CASE PERMDENIED WHEN 1 THEN 'SELECT'
WHEN 2 THEN 'UPDATE'
WHEN 3 THEN 'SELECT,UPDATE'
WHEN 4 THEN 'REFERENCES'
WHEN 5 THEN 'SELECT, REFERENCES'
WHEN 6 THEN 'UPDATE,REFERENCES'
WHEN 7 THEN 'SELECT,UPDATE,REFERENCES'
WHEN 8 THEN 'INSERT'
WHEN 9 THEN 'SELECT,INSERT'
WHEN 10 THEN 'UPDATE,INSERT'
WHEN 11 THEN 'SELECT,UPDATE,INSERT'
WHEN 12 THEN 'REFERENCES,INSERT'
WHEN 13 THEN 'SELECT,REFERENCES,INSERT'
WHEN 14 THEN 'UPDATE,REFERENCES,INSERT'
WHEN 15 THEN 'SELECT,UPDATE,REFERENCES,INSERT'
WHEN 16 THEN 'DELETE'
WHEN 17 THEN 'SELECT,DELETE'
WHEN 18 THEN 'UPDATE,DELETE'
WHEN 19 THEN 'SELECT,UPDATE,DELETE'
WHEN 20 THEN 'REFERENCES,DELETE'
WHEN 21 THEN 'SELECT,REFERENCES,DELETE'
WHEN 22 THEN 'UPDATE,REFERENCES,DELETE'
WHEN 23 THEN 'SELECT,UPDATE,REFERENCES,DELETE'
WHEN 24 THEN 'INSERT,DELETE'
WHEN 25 THEN 'SELECT,INSERT,DELETE'
WHEN 26 THEN 'UPDATE,INSERT,DELETE'
WHEN 27 THEN 'SELECT,UPDATE,INSERT,DELETE'
WHEN 28 THEN 'REFERENCES,INSERT,DELETE'
WHEN 29 THEN 'SELECT,REFERENCES,INSERT,DELETE'
WHEN 30 THEN 'REFERENCES,INSERT,DELETE'
WHEN 31 THEN 'SELECT,UPDATE,REFERENCES,INSERT,DELETE'
WHEN 32 THEN 'EXECUTE' ELSE NULL END+' on '+OBJECTNAME+' to '+USERNAME FROM #dbusers WHERE PERMDENIED <> 0
DROP TABLE #DBUSERS 

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *