Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get all the domain and SQL users from an instance, per database


Get all the domain and SQL users from an instance, per database

Author
Message
franck.maton
franck.maton
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 344
Hello,

For SQL 2k5, 2K8 and 2K8R2, I need a T-SQL query to get all the users listed in:
SSMS => "Instance" => "DB" => "Security" => "Users" Except 'dbo','guest','sys','INFORMATION_SCHEMA' for all the users databases of my instances. I hope it's clear enough :-)

I guess I have to use sp_MSForEachDB but I can't find myself query that returns just what I want. I have always too much informations/users in the resultset...

Anyone has had to write it once ?

Thanks in advance.
Regards.
Franck.
Robert klimes
Robert klimes
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1849 Visits: 3408
this should get you what you want per database (without excluding any users)

select name from sys.database_principals
where type in ('S','G','U')



give it a try with sp_msforeachdb and post back if you are still having issues.

Bob
-----------------------------------------------------------------------------
How to post to get the best help
franck.maton
franck.maton
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 344
First of all, thanks for your reply.

This:
exec sp_MSforeachdb 'select name 
from sys.database_principals
where type in (''S'',''G'',''U'')
and name not in (''dbo'',''guest'',''sys'',''INFORMATION_SCHEMA'',''##MS_PolicyEventProcessingLogin##'')'



Always give me 2 results for all the databases, per instance, though I've a lot more users mapped to my DB's.

To be clear, on instance A, i've the users 1,2,3,4,5 mapped to different databases in the instance.
On instance B, i've the users 6,7,8,9,10 mapped to the different databases.

The resultset of instance A is:

DB1: 1,2
DB2: 1,2
DB3: 1,2
.
.
.

Though user 4 and 5 have also access to DB1 and DB2... for example.


On instance B:

DB1: 7,8
DB2: 7,8
DB3: 7,8
.
.
.

Though user 9 and 10 have also access to DB1 and DB2... for example.

I don't know if it's normal...

I hope this message is clear enough :-).

Franck.
Robert klimes
Robert klimes
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1849 Visits: 3408
you have to qualify the database in the query with a ?. you can also load the result into a table to make them searchable

CREATE TABLE #tmp(dbname VARCHAR(256),username VARCHAR(256))

INSERT #tmp EXEC sp_MSforeachdb 'select ''?'',name
from [?].sys.database_principals
where type in (''S'',''G'',''U'')
and name not in (''dbo'',''guest'',''sys'',''INFORMATION_SCHEMA'',''##MS_PolicyEventProcessingLogin##'')'

SELECT *
FROM #tmp



Bob
-----------------------------------------------------------------------------
How to post to get the best help
franck.maton
franck.maton
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 344
Hey, this looks like what i was expecting to get !

Thanks a lot :-) !
sdelachapelle
sdelachapelle
Valued Member
Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)Valued Member (65 reputation)

Group: General Forum Members
Points: 65 Visits: 1598
Hi,

I would not rely too much on sp_MSforeachdb because it might skip one or several databases on a busy server.
Also I would not skip dbo either, dbo can be mapped to a login other than sa therefore, when it happens you want to know about it.

Please find below the query I use to get all the databases users:



DECLARE @SQLCMDDBUser NVARCHAR(MAX)

IF object_id('tempdb..#DBUserTable','U') IS not NULL DROP TABLE #DBUserTable
CREATE TABLE #DBUserTable
([ServerName] [sysname] NOT NULL,
[DBName] [sysname] NOT NULL,
[UserName] [sysname] NOT NULL,
[MappedLogin] [nvarchar](128) NULL,
[UserType] [nvarchar](60) NOT NULL,
[CreateDate] [datetime] NULL,
[ModifyDate] [datetime] NULL,
[IsOrphan] [int] NOT NULL)

SELECT @SQLCMDDBUser=coalesce(@SQLCMDDBUser,'') + CHAR(13) + CHAR(10) + ' use ' + QUOTENAME([name]) + ';
INSERT INTO #DBUserTable
SELECT
@@ServerName As ServerName,
DB_NAME() As DBName,
DP.name As UserName,
SP.name As MappedLogin,
DP.type_desc AS UserType,
DP.create_date As CreateDate,
DP.modify_date As ModifyDate,
Case
WHEN SP.sid Is Null THEN 1
ELSE 0
END As IsOrphan
FROM ' + QUOTENAME([name]) + '.sys.database_principals DP LEFT JOIN master.sys.server_principals SP ON DP.sid = SP.sid
WHERE
DB_Name() NOT IN (''model'',''tempdb'') AND
DP.type NOT IN (''R'')AND
DP.is_fixed_role <> 1 AND
DP.sid NOT IN (0x01,0x00) AND
DP.sid IS NOT NULL AND
DP.name NOT LIKE ''##%''
ORDER BY DBName,UserName;' FROM master.sys.databases WHERE name NOT IN ('tempdb','model') AND state_desc = 'ONLINE' ORDER BY name

EXECUTE(@SQLCMDDBUser)

SELECT * FROM #DBUserTable

DROP TABLE #DBUserTable





Regards.
Ralph Thomas
Ralph Thomas
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 508
I played with all the scripts on this page and have looked before for something like this. This is beautiful.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search