SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Finding Objects Owned by non-DBO Users

By Santveer Singh, 2004/12/15

Total article views: 5305 | Views in the last 30 days: 14

In this article I will show how can we get the list of all the objects (table, Procedure, view or user defined function) owned by non DBO users. I believe this occurred most on Development server where developers doesn’t have DBO rights in Database. To get the list of object we need to create below table and procedure.

Step 1: Create table ObjList using below script:

CREATE
TABLE [ObjList] (
	[DBName]
[sysname] NOT NULL ,
	[Object_Type]
[varchar] (20)  NULL ,
	[ObjectOwner]
[sysname] NOT NULL ,
	[ObjectName]
[sysname] NOT NULL ,
	[cur_date]
[datetime] NOT NULL CONSTRAINT [DF_ObjList_cur_date] DEFAULT
(getdate())
)
ON [PRIMARY]
GO

Step 2: Create Procedure Object_owned_by_non_dbo using below script

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
Create Procedure Object_owned_by_non_dbo
as
declare @dbname varchar(200)
declare @mSql1	varchar(8000)
Set nocount on

DECLARE DBName_Cursor CURSOR FOR 
	select name 
	from
	master.dbo.sysdatabases 
	where name not in ('master','msdb','tempdb')
	Order by name

OPEN DBName_Cursor

FETCH NEXT FROM DBName_Cursor INTO @dbname

WHILE @@FETCH_STATUS = 0
 BEGIN 
  Set @mSql1 = 'Insert into  ObjList (DBName, Object_Type, ObjectOwner,ObjectName)'+char(13)
  Set @mSql1 = @mSQL1+'Select '''+@dbname+''' as dbName,ObjType = 
	Case xtype 
		when ''u'' then ''Table''
		when ''V'' then ''View''
		when ''P'' then ''Procedure''
		when ''FN'' then ''UD Function''
		else xtype end
	, SU.name,SO.name  from '+@dbname+'.dbo.sysobjects SO join '+@dbname+'.dbo.sysusers SU 
	on SO.uid = SU.uid and  su.name <> ''dbo''  
	and SO.xtype in (''u'',''v'',''p'',''FN'')'

  --Print @mSql1
  Execute (@mSql1)

  FETCH NEXT FROM DBName_Cursor INTO @dbname 
 END

CLOSE DBName_Cursor
DEALLOCATE DBName_Cursor
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Step 3: Please execute the below script to retrieve the list.

Select * from ObjList


DBName      Object_Type  ObjectOwner    ObjectName       cur_date
----------  ------------ -------------  ---------------- -----------------------
Database1   UD Function  Userv1         IS_FIELD_IN      2004-10-29 16:42:39.127
Database2   Table        Domain1\NT1    Vendor_Top200    2004-10-29 16:42:39.813
Database2   Table        Domain1\NT1    Supplier_Top200  2004-10-29 16:42:39.813
Database2   Table        Domain1\NT1    Emp_Top200       2004-10-29 16:42:39.813

Step 3: We can confined the list for specific user or database as below:

Select * from ObjList where DBName = 'Database1'

DBName     Object_Type    ObjectOwner    ObjectName        cur_date               
---------- -------------- -------------- ----------------- -----------------------
Database1  UD Function    Userv1         IS_FIELD_IN       2004-10-29 16:42:39.127

Or

Select * from ObjList where ObjectOwner = 'Domian1\NT1'

DBName     Object_Type    ObjectOwner    ObjectName      cur_date               
---------- -------------- -------------- --------------- -----------------------
Database1  UD Function    Userv1         IS_FIELD_IN     2004-10-29 16:42:39.127
Database2  Table          Domain1\NT1    Vendor_Top200   2004-10-29 16:42:39.813
Database2  Table          Domain1\NT1    Supplier_Top200 2004-10-29 16:42:39.813
Database2  Table          Domain1\NT1    Emp_Top200      2004-10-29 16:42:39.813

I think you will like this procedure. Please don’t forget to send me your comments on this article.

By Santveer Singh, 2004/12/15

Total article views: 5305 | Views in the last 30 days: 14
Your response
 
 
Related Articles
FORUM

Cursor - passing @dbname to INSERT STATEMENT

Concatentaion of @dbname from cursor + table name to use in FROM clause

FORUM

Invalid object name 'syssubscriptions'- when running sp_removedbreplication 'dbname'

Invalid object name 'syssubscriptions'- when running sp_removedbreplication 'dbname'

FORUM

query(dbname,size,recoverymodel)

query(dbname,size,recoverymodel)

FORUM

query(dbname,size,recoverymodel)

query(dbname,size,recoverymodel)

FORUM

Cursor

cursor

Tags
 
Contribute

Free registration required...

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Login (existing users)

Login

Email:   Password:   Remember me: Forgotten your password?

Register (new users)

Register

Email:   Password:
Confirm:

Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

Steve Jones
Editor, SQLServerCentral.com