开发者

query for databases and privileges

ok.. i am us开发者_StackOverflow社区ing sql server 2008... i want a sql query for finding different databases created by a particular login user??? for eg. lets say user1 has created databases db1 and db2. and user2 is created databases db3 and db4.. than the query should give result db1,db2 for user1 and db3,db4 for user2...

also i want query for getting the privileges of a particular user???


The closest I can think of is the current owner, which is very often the creator of a database. You can query that like:

select  db.name
,       l.name
from    sys.databases db
join    sys.syslogins l
on      db.owner_sid = l.sid

To query for a comma-separated list per user, try:

select  l.name
,       stuff(db.list,len(db.list),1,'')
from    sys.syslogins l
cross apply
        (
        select  db.name + ','
        from    sys.databases db
        where   db.owner_sid = l.sid
        for xml path('')
        ) db(list)
where   db.list is not null


well I don't know why do you need to query the user owner for each database but I hope this help you : SELECT name, SUSER_SNAME(owner_sid) FROM sys.databases

SUSER_SNAME() well retireve the user name

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜