Important alert: (current site time 7/15/2013 7:44:26 PM EDT)
 

VB icon

DB Object Report

Email
Submitted on: 6/28/2001 9:49:14 PM
By: MisterD 
Level: Beginner
User Rating: By 2 Users
Compatibility: SQL Server 7.0, Other
Views: 13446
 
     Script to report on number of foreign keys, primary keys, indexes, triggers, views, tables (user and system), stored procedures, defaults and rules. Great for comparing two or more DB's against a known master copy, etc.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: DB Object Report
-- Description:Script to report on number of foreign keys, primary keys, indexes, triggers, views, tables (user and system), stored procedures, defaults and rules. Great for comparing two or more DB's against a known master copy, etc.
-- By: MisterD
--
-- Returns:Object counts of each type, as well as the total object count at the bottom of the report.
--
-- Assumes:Just fire it on each DB you want to compare results from, that's it!
--
-- Side Effects:More free time, due to not having to manually guess at the schema differences between two or more DB's anymore! :)
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=316&lngWId=5--for details.--**************************************

SET NOCOUNT ON
DECLARE	@Output		char( 255 ),
	@Total		int,
	@Count		int
PRINT	'====================================================================='
SELECT	@Output = ' Object Check on ' + RTRIM( DB_Name() )
	+ ' as of ' + RTRIM( CONVERT( char( 19 ), GetDate() ) )
PRINT	@Output
PRINT	'====================================================================='
PRINT	''
SELECT	@Total	= 0,
	@Count	= 0
-- Count Stored Procedures
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'P'
SELECT	@Output = 'Stored Procedures : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count System tables
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'S'
SELECT	@Output = 'System Tables : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Foreign keys
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'F'
SELECT	@Output = 'Foreign Keys : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Primary keys
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'PK'
SELECT	@Output = 'Primary Keys : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count User tables
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'U'
SELECT	@Output = 'User Tables: '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Triggers
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'TR'
SELECT	@Output = 'Triggers : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Defaults
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'D'
SELECT	@Output = 'Default: '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Indexes
SELECT	@Count = COUNT(*)
FROM	SysIndexes
WHERE	IndID > 0 
AND	IndID < 255
AND	( Status & 64 )= 0
AND	( Status & 8388608 ) = 0
AND	( Status & 16777216 ) = 0
SELECT	@Output = 'Indexes: '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Rules
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'R'
SELECT	@Output = 'Rules : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
-- Count Views
SELECT	@Count = COUNT(*)
FROM	SysObjects
WHERE	XType = 'V'
SELECT	@Output = 'Views : '
	+ RTRIM( CONVERT( char( 12 ), @Count ) )
PRINT	@Output
SELECT	@Total = @Total + @Count
PRINT	''
PRINT	'====================================================================='
SELECT	@Output = 'Total Objects : '
	+ RTRIM( CONVERT( char( 12 ), @Total ) )
PRINT	@Output
PRINT	'====================================================================='
SET NOCOUNT OFF


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.