Technical Article

Find all objects in Server Owned by a Login

,

In response to a request for a script to show all of the objects on a server onwed by a Login. I didn't know of any tool built-in to do this, so I wrote the following proc (SQL Server 2005 only).

Note the following featues also:

  1. No cursors or other Loops (but total number of databases is limitedt to 255).
  2. If the DB User for a Login is the explicit Owner for an object, then that will be reported.
  3. If there is no explicit owner for an object, then the Login of the DB User that owns the schema containing the object will be returned as the owner.
  4. If there is no explicit owner for an object and the object is contained in the [dbo] schema, then the Login that owns the database will be returned as the object's owner (this is the correct method of assesing object ownership in SQL server 2005).

If the Dynamic SQL concerns you, then notice that the Login name filter is only applied at the end. This means that you could also take the PRINT output and turn it into a static View. If you leave off the Login name WHERE clause, then this view returns every SQL object in your server with it's proper owner. This static View cna be preserved and reused as-is so long as your database configuration does not change.

You could write a SQL Server 2000 version of this also, but it would be significantly different, because of the large changes in the Schema/Owner security model, though probably simpler.

CREATE Proc spLogin_OwnedObjects ( @Login as SYSNAME ) As
/*
        Display all objects in all DBs owned by the Login.

2008-07-06 RBarryYoung  Created.
2008-08-28 RBarryYoungAdded corrections for DBs with different Collations
(note that ReportingDBs have different Collations)

Test:
 EXEC spLogin_OwnedObjects 'sa'
*/    declare @sql varchar(MAX), @DB_Objects varchar(MAX)
    Select @DB_Objects = ' L.name COLLATE DATABASE_DEFAULT as Login, U.Name  COLLATE DATABASE_DEFAULT as [User]
, o.name COLLATE DATABASE_DEFAULT as [name]
, o.object_id
, o.principal_id
, o.schema_id
, o.parent_object_id
, o.type COLLATE DATABASE_DEFAULT as [type]
, o.type_desc COLLATE DATABASE_DEFAULT as [type_desc]
, o.create_date
, o.modify_date
, o.is_ms_shipped
, o.is_published
, o.is_schema_published
     From %D%.sys.objects o
      Join %D%.sys.database_principals u 
        ON Coalesce(o.principal_id
 , (Select S.Principal_ID from %D%.sys.schemas S Where S.Schema_ID = O.schema_id))
            = U.principal_id
      Left Join %D%.sys.server_principals L on L.sid = u.sid
'

    Select @sql = 'SELECT * FROM
    (Select '+Cast(database_id as varchar(9))+' as DBID, ''master'' as DBName, '
                     + Replace(@DB_objects, '%D%', [name])
     From master.sys.databases
     Where [name] = 'master'

    Select @sql = @sql + 'UNION ALL Select '+Cast(database_id as varchar(9))+', '''+[name]+''', '
                     + Replace(@DB_objects, '%D%', [name])
     From master.sys.databases
     Where [name] != 'master'

    Select @sql = @sql + ') oo  Where Login = ''' + @Login + ''''

    print @sql
    EXEC (@sql)

Rate

4.1 (10)

You rated this post out of 5. Change rating

Share

Share

Rate

4.1 (10)

You rated this post out of 5. Change rating