Solved

SQL 2008

Posted on 2013-12-27
4
242 Views
Last Modified: 2014-01-16
Hi,

I am trying to get a SQL script to list out all the users for a database.  Please let me know the best way to export the security user information.
0
Comment
Question by:Jack_son_
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:vr6r
vr6r earned 333 total points
Comment Utility
Try this - returns users for the db in your current connection:
SELECT * FROM sys.database_principals

Open in new window

0
 

Author Comment

by:Jack_son_
Comment Utility
will this pull all of the Security users?
0
 
LVL 8

Assisted Solution

by:vr6r
vr6r earned 333 total points
Comment Utility
Yes, relevant to your current database connection.  For example, if you open a connection on your sql server to database "test_db" and run that command, it will give you all the defined users to "test_db".
0
 
LVL 9

Accepted Solution

by:
QuinnDex earned 167 total points
Comment Utility
;with ServerPermsAndRoles as
(
    select
        spr.name as principal_name,
        spr.type_desc as principal_type,
        spm.permission_name collate SQL_Latin1_General_CP1_CI_AS as security_entity,
        'permission' as security_type,
        spm.state_desc
    from sys.server_principals spr
    inner join sys.server_permissions spm
    on spr.principal_id = spm.grantee_principal_id
    where spr.type in ('s', 'u')

    union all

    select
        sp.name as principal_name,
        sp.type_desc as principal_type,
        spr.name as security_entity,
        'role membership' as security_type,
        null as state_desc
    from sys.server_principals sp
    inner join sys.server_role_members srm
    on sp.principal_id = srm.member_principal_id
    inner join sys.server_principals spr
    on srm.role_principal_id = spr.principal_id
    where sp.type in ('s', 'u')
)
select *
from ServerPermsAndRoles
order by principal_name

Open in new window


Basically what it does it gets the permissions granted and denied, and unions it with the role membership. It should give you a brief look on the security for server logins.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now