Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

how to check the Schema Objects Details in Each Database using SP Expand / Collapse
Author
Message
Posted Tuesday, April 15, 2008 12:26 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Saturday, September 12, 2009 9:12 PM
Points: 65, Visits: 224
Also I would need couple of details ,could you help me
---
This is what I needed,
1)Objects which do not belong to DBO

This is the query we are using to find out

select schema_name(schema_id)as ObjectName, name,type,type_desc from sys.objects
where schema_name(schema_id) <> 'dbo'
and type in('U','P','V', 'FN','TF','IF','PK','UQ')
go

2)Database users which do not have DBO as their default schema

SELECT *
FROM sys.database_principals
WHERE [type] IN ('U','S')
AND ISNULL(default_schema_name,'')<>'dbo'

We would like to create a report out of it and let a procedure run in every instance on a daily basis. The procedures should write the information into seperate tables in theOne of DB(Like ABC)database
I need to have the Sp in one Database and it needs to check all the Database and fetch the details into separate tables as per the database name

Could you please help me
Post #484782
Posted Tuesday, April 15, 2008 7:40 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, June 19, 2014 6:51 AM
Points: 1,264, Visits: 723
Hi,
You may use these two ways:

1) Build dynamic query:

SET NOCOUNT ON

DECLARE @db NVARCHAR (50), @sqlStr NVARCHAR(4000)
DECLARE @tbl_dbs TABLE (dbName NVARCHAR(50))

INSERT @tbl_dbs SELECT Name from master.sys.databases

WHILE EXISTS
(SELECT dbName FROM @tbl_dbs)

BEGIN

SELECT TOP 1 @db = dbName
, @sqlStr = 'INSERT ABC..YourTable (ObjectName, name,type,type_desc ) SELECT schema_name(schema_id)as ObjectName, name,type,type_desc
from ' + @db + '.sys.objects
where schema_name(schema_id) <> ''dbo''
and type in(''U'',''P'',''V'', ''FN'',''TF'',''IF'',''PK'',''UQ'')'
FROM @tbl_dbs

EXEC master.dbo.sp_executesql @sqlStr

DELETE FROM @tbl_dbs
WHERE dbName = @db

END

2) Use undocumented stored procedure (which is not actually recommended):

EXEC sp_MSforeachDB 'INSERT ABC..YourTable (ObjectName, name,type,type_desc )
SELECT schema_name(schema_id)as ObjectName, name,type,type_desc
from ?.sys.objects
where schema_name(schema_id) <> ''dbo''
and type in(''U'',''P'',''V'', ''FN'',''TF'',''IF'',''PK'',''UQ'')'
Post #485377
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse