Copy Permissions for a database

  • Comments posted to this topic are about the item Copy Permissions for a database

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • The function bo.f_lookup_system_id is not defined and the link is no longer working.

    Any suggestions on where I can find this function?:)


  • I got the same thing, missing f_lookup_system_id.

    Can I get that code?

  • Gosh. This is a long time ago.

    I'd forgotten all about this until I got an email from smorgan just now. And I can barely remember anything about it, so my starting point is not that much different from you guys.

    I did quite a bit of digging but didn't find much which is directly useful.

    I can't be sure, but I think this article might now be a link for the one referred to:

    Given that, I think the f_lookup_system_id function probably looked something like this (this is a guess, but it should at least give the idea):

    create function dbo.f_lookup_system_id(@name varchar(50), @id int) returns varchar(50) as


    select dbo.f_lookup_system_id('sysprotects_protecttype', 206)

    select dbo.f_lookup_system_id('sysprotects_action', 197)



    declare @S varchar(50)

    if @name = 'sysprotects_protecttype'

    select @S =

    case @id

    when 204 then 'GRANT_W_GRANT'

    when 205 then 'GRANT'

    when 206 then 'REVOKE'


    if @name = 'sysprotects_action'

    select @S =

    case @id

    when 26 then 'REFERENCES'

    when 178 then 'CREATE FUNCTION'

    when 193 then 'SELECT'

    when 195 then 'INSERT'

    when 196 then 'DELETE'

    when 197 then 'UPDATE'

    when 198 then 'CREATE TABLE'

    when 203 then 'CREATE DATABASE'

    when 207 then 'CREATE VIEW'

    when 222 then 'CREATE PROCEDURE'

    when 224 then 'EXECUTE'

    when 228 then 'BACKUP DATABASE'

    when 233 then 'CREATE DEFAULT'

    when 235 then 'BACKUP LOG'

    when 236 then 'CREATE RULE'


    return @S


    Sorry I can't be more helpful.

    If you manage to get anything useful out of this, please let us know! 🙂

    Ryan Randall

    Solutions are easy. Understanding the problem, now, that's the hard part.

  • Thanks! I may need to get back into that.

    For now, I used some of your code in the previous article and modified it. I am trying to copy Roles (and some users) from one DB to another on the same server. Here's what I got to work for me (this is a SQL 2000 server):

    [font="Courier New"]---------------------------------------------------------------


    --from 1 db on this server to another db on this server



    -- change all CA_AugME to "from" DataBase(OLD db)

    -- change all CA_SeptME to "to" DataBase(NEW db)


    -- If the user had no permissions to DB, this will copy that from the "from" db and

    -- print a msg in grid 'ADDED USER ', @user_name


    -- All Roles on the "from" db will be applied to the "to" db


    -- based on code from


    -- Copy Roles

    USE CA_SeptME

    declare @S varchar(1000)

    DECLARE c_from_user_roles CURSOR



    select as UserName, as RoleName --, *

    from CA_AugME.dbo.sysusers as u

    join CA_AugME.dbo.sysmembers AS Person

    on u.uid = Person.memberuid

    JOIN CA_AugME.dbo.sysusers AS a_ROLE

    on a_ROLE.uid = groupuid



    from CA_SeptME.dbo.sysusers as u

    join CA_SeptME.dbo.sysmembers AS Person

    on u.uid = Person.memberuid

    JOIN CA_SeptME.dbo.sysusers AS a_ROLE

    on a_ROLE.uid = groupuid)

    --AND U.NAME = 'jduffy'-- TEST only !

    DECLARE @role_name varchar(40)

    DECLARE @user_name varchar(40)

    OPEN c_from_user_roles

    FETCH NEXT FROM c_from_user_roles INTO @user_name, @role_name

    WHILE (@@fetch_status <> -1)


    IF (@@fetch_status <> -2)



    if NOT exists (select * from CA_SeptME.dbo.sysusers where name = @user_name)


    exec sp_grantdbaccess @user_name-- add user to DB

    -- print 'ADDED USER ' @user_name

    SELECT 'ADDED USER ', @user_name




    set @S = 'exec sp_addrolemember ''' + @role_name + ''', ''' + @user_name + ''''

    print @S

    exec(@s)-- copy Role for user



    FETCH NEXT FROM c_from_user_roles INTO @user_name, @role_name


    CLOSE c_from_user_roles

    DEALLOCATE c_from_user_roles[/font]

  • works great with the function as was posted.

    I just added double quotes on the line #92:

    set @S = @protect_type_name + ' ' + @action_name + ' ON ' + @object_name + ' TO "' + @to_user + '"'

    to support "DOMAIN\user" format.


    Roberto Figueroa.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply