Click here to Skip to main content
Click here to Skip to main content

Get SQL Table Information

By , 19 Jul 2009
 

Introduction

One of the common requirements is to view the Table information of SQL server database. Here is the easiest way to view the SQL Table Information.

TableInfo.JPG

Output figure

Background

Couple of days ago, one of my friends asked me why we always use the ADO.NET class library to get any SQL Table information runtime! SQL Server provides several "standard" techniques for it. So I decided to try it and I found it’s really cool and I believe that it is always better to use the standard techniques provided by SQL Server where possible.

This is easy enough to do with cursors and dynamic SQL, but after looking at how “sp_ spaceused” worked and how SMO gets the same information, I decided that I could write it without either.

Even better, once I wrote it through, I realized that it could easily be rewritten as a StoredProcedure. Now I could reuse it by joining it with other tables and views in new queries or procedures whenever I wanted.

Using the Code

I exercise StoredProcedure “sp_spaceused” which displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or service broker queue in the current database, or displays the disk space reserved and used by the whole database. In other words, this StoredProcedure computes the amount of disk space used for data and indexes, and the disk space used by a table in the current database. A sample code is given below:

            
/****** Object:  StoredProcedure [dbo].[spTableInformation]    
Script Date: 03/05/2009 21:33:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:        <Author,,Md. Marufuzzaman>
-- Create date: <Create Date,,03/05/2009>
-- Description:    <Description,,>
--Thanks to ,, RBarry Young
-- =============================================
CREATE PROCEDURE [dbo].[spTableInformation]
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

SELECT     SCHEMA_NAME(SYSTBL.SCHEMA_ID) AS [SCHEMA], SYSTBL.NAME, COALESCE
                          ((SELECT     NAME
                              FROM         sys.database_principals AS SDBP
                              WHERE     (PRINCIPAL_ID = SYSTBL.PRINCIPAL_ID)), _
				SCHEMA_NAME(SYSTBL.SCHEMA_ID)) AS OWNER, _
				SYSTBL.MAX_COLUMN_ID_USED AS COLUMNS, 
                      CAST(CASE SINDX_1.INDEX_ID WHEN 1 THEN 1 ELSE 0 END AS BIT) _
				AS HASCLUSIDX, COALESCE
                          ((SELECT     SUM(rows) 
                              FROM         sys.partitions AS SPART
                              WHERE     (OBJECT_ID = SYSTBL.OBJECT_ID) AND _
				(INDEX_ID < 2)), 0) AS [ROWCOUNT], COALESCE
                          ((SELECT     CAST(SPTV.low / 1024.0 AS FLOAT) 
                                                    * SUM(SAU_1.USED_PAGES - _
						CASE WHEN SAU_1.TYPE <> 1 _
						THEN SAU_1.USED_PAGES WHEN _
						SYSP.INDEX_ID < 2 THEN _
						SAU_1.DATA_PAGES
                                                     ELSE 0 END) 
                              FROM         sys.indexes AS SINDX_2 INNER JOIN
                                                    sys.partitions AS SYSP ON _
						SYSP.OBJECT_ID = _
						SINDX_2.OBJECT_ID _
						AND SYSP.INDEX_ID = _
						SINDX_2.INDEX_ID INNER JOIN
                                                    sys.allocation_units AS SAU_1 ON _
						SAU_1.CONTAINER_ID = _
						SYSP.PARTITION_ID
                              WHERE     (SINDX_2.OBJECT_ID = SYSTBL.OBJECT_ID)), 0.0) _
						AS INDEXKB, COALESCE
                          ((SELECT     CAST(SPTV.low / 1024.0 AS FLOAT) 
                                                    * SUM(CASE WHEN SAU_2.TYPE <> 1 _
						THEN SAU_2.USED_PAGES _
						WHEN SYSP.INDEX_ID < 2 _
						THEN SAU_2.DATA_PAGES ELSE 0 END) 
                                                    AS Expr1
                              FROM         sys.indexes AS SINDX_2 INNER JOIN
                                                    sys.partitions AS SYSP ON _
						SYSP.OBJECT_ID = _
						SINDX_2.OBJECT_ID _
						AND SYSP.INDEX_ID = _
						SINDX_2.INDEX_ID INNER JOIN
                                                    sys.allocation_units AS SAU_2 _
						ON SAU_2.CONTAINER_ID = _
						SYSP.PARTITION_ID
                              WHERE     (SINDX_2.OBJECT_ID = SYSTBL.OBJECT_ID)), 0.0) _
				AS DATAKB, SYSTBL.CREATE_DATE, SYSTBL.MODIFY_DATE
FROM         sys.tables AS SYSTBL INNER JOIN
                      sys.indexes AS SINDX_1 ON SINDX_1.OBJECT_ID = SYSTBL.OBJECT_ID _
						AND SINDX_1.INDEX_ID < 2 INNER JOIN
                      master.dbo.spt_values AS SPTV ON SPTV.NUMBER = 1 AND SPTV.type = 'E'
                      
END

GO

Conclusion

I hope that this article might be helpful to you. Enjoy!

History

  • 19th July, 2009: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Md. Marufuzzaman
CEO
Bangladesh Bangladesh
Member
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
 
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
 
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
 
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
 
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberSaeid.Babaei8621 Feb '12 - 20:38 
GeneralMy vote of 2memberjpaulino3 Aug '09 - 9:14 
GeneralRe: My vote of 2mvpMd. Marufuzzaman5 Oct '11 - 23:05 
GeneralMy vote of 2memberVMykyt19 Jul '09 - 11:25 
GeneralRe: My vote of 2groupMd. Marufuzzaman26 Jul '09 - 5:10 
GeneralMy vote of 2mvpSacha Barber19 Jul '09 - 8:46 
GeneralRe: My vote of 2groupMd. Marufuzzaman26 Jul '09 - 5:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Jul 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid