Scripting SQL server security

"Chris Wuestefeld" <chris-/ffxFymC14jQ7bhM+Ce/[email protected]>
Newsgroups gmane.comp.windows.off-topic
Message-ID <010901ca8a56$59c69ed0$0d53dc70$@com>
This morning I had a minor disaster on my development DB, nuking all of the
permissions for access to my stored procedures. I wanted to recreate them
from our production server, but had a heck of a time figuring out how to do
this. I couldn't find anything I could directly plagiarize from Google, so I
had to invent it myself. It turns out that if you understand the various
catalog views, it's straightforward, but it took a bit of research to get
there. 

I thought I'd post my result, in case anyone else finds it useful. This
query dumps out a bunch of GRANT commands, so that you can copy them back
into another query window and execute them on the server that's to receive
the permissions.

SELECT 'GRANT ' + dp.permission_name collate latin1_general_cs_as

      + ' ON ' + s.name + '.' + o.name + ' TO ' + dpr.name 

    FROM sys.database_permissions AS dp

    INNER JOIN sys.objects AS o ON dp.major_id=o.object_id

    INNER JOIN sys.schemas AS s ON o.schema_id = s.schema_id

    INNER JOIN sys.database_principals AS dpr ON
dp.grantee_principal_id=dpr.principal_id

    WHERE dpr.name NOT IN ('public','guest')

    AND permission_name='EXECUTE'

Of course, you might want to comment out the last line if you need to dump
permissions for objects other than stored procedures.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.