Count

TSQL Count Number Of Stored Procedures, Views, Tables or Functions

By | Code, Database, MSSQL2005, MSSQL2008, Software Development, SQL Server, TSQL | 16 Comments

You can use TSQL to Count Number Of Stored Procedures, Views, Tables or Functions in a Database by using the Database INFORMATION_SCHEMA view

/* Count Number Of Tables In A Database */
    SELECT COUNT(*) AS TABLE_COUNT FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_TYPE='BASE TABLE'
    /* Count Number Of Views In A Database */
    SELECT COUNT(*) AS VIEW_COUNT FROM INFORMATION_SCHEMA.VIEWS
    /* Count Number Of Stored Procedures In A Database */
    SELECT COUNT(*) AS PROCEDURE_COUNT FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE'
    /* Count Number Of Functions In A Database */
    SELECT COUNT(*) AS FUNCTION_COUNT FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION'

The same methodology can be used to query for information :

/* Select Table Information For A Database */
    SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
    /* Select View Information For A Database */
    SELECT * FROM INFORMATION_SCHEMA.VIEWS
    /* Select Stored Procedure Information For A Database */
    SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE'
    /* Select Function Information For A Database */
    SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION'