How to count total number of stored procedure and tables in SQL Server 2008

冷暖自知 提交于 2019-11-30 00:05:15

This will give you the count of tables and stored procedures.

SELECT 
    CASE TYPE 
        WHEN 'U' 
            THEN 'User Defined Tables' 
        WHEN 'S'
            THEN 'System Tables'
        WHEN 'IT'
            THEN 'Internal Tables'
        WHEN 'P'
            THEN 'Stored Procedures'
        WHEN 'PC'
            THEN 'CLR Stored Procedures'
        WHEN 'X'
            THEN 'Extended Stored Procedures'
    END, 
    COUNT(*)     
FROM SYS.OBJECTS
WHERE TYPE IN ('U', 'P', 'PC', 'S', 'IT', 'X')
GROUP BY TYPE

You can find in sys.objects all types of objects in the database. You will have to run this query on each of your databases to see the count of objects.

You can find all information about what is stored in sys.objects here.

Szymon

You can use those 2 queries:

select count(*) as TablesCount from sys.tables
select count(*) as ProceduresCount from sys.procedures

I often use this script I found on this blog

USE [MyDatabase]
GO
SELECT 'Count' = COUNT(*), 'Type' = CASE type 
                WHEN 'C' THEN 'CHECK constraints' 
                WHEN 'D' THEN 'Default or DEFAULT constraints' 
                WHEN 'F' THEN 'FOREIGN KEY constraints' 
                WHEN 'FN' THEN 'Scalar functions' 
                WHEN 'IF' THEN 'Inlined table-functions' 
                WHEN 'K' THEN 'PRIMARY KEY or UNIQUE constraints' 
                WHEN 'L' THEN 'Logs' 
                WHEN 'P' THEN 'Stored procedures' 
                WHEN 'R' THEN 'Rules' 
                WHEN 'RF' THEN 'Replication filter stored procedures' 
                WHEN 'S' THEN 'System tables' 
                WHEN 'TF' THEN 'Table functions' 
                WHEN 'TR' THEN 'Triggers' 
                WHEN 'U' THEN 'User tables' 
                WHEN 'V' THEN 'Views' 
                WHEN 'X' THEN 'Extended stored procedures' 
    END 

    FROM sys.objects 
    GROUP BY type 
    ORDER BY type 
GO

You can modify it by type from information about sys.objects

Or by object from this reference Object Catalog Views, as you already got an answer for tables and procedures in the previous answers, e.g.

SELECT count(*) AS MyTables FROM sys.tables
SELECT count(*) AS MyProcedures FROM  sys.procedures
SELECT count(*) AS MyTriggers FROM  sys.triggers
SELECT count(*) AS MyViews FROM  sys.views

Hope this gives you some additional help

Use the following queries.

USE YOURDBNAME
SELECT COUNT(*) AS totalTable from information_schema.tables 
WHERE table_type = 'base table'  

select Count(*) AS TotalProc from sys.procedures

I now use the below, based on Milica's answer with some extra types, default value and sorted by count.

SELECT 'Count' = COUNT(*), 'Type' = CASE type 
    WHEN 'AF' THEN 'Aggregate function (CLR)' 
    WHEN 'C' THEN 'CHECK constraints' 
    WHEN 'D' THEN 'Default or DEFAULT constraints' 
    WHEN 'F' THEN 'FOREIGN KEY constraints' 
    WHEN 'FN' THEN 'Scalar functions' 
    WHEN 'FS' THEN 'Assembly (CLR) scalar-function' 
    WHEN 'FT' THEN 'Assembly (CLR) table-valued function' 
    WHEN 'IF' THEN 'Inlined table-functions' 
    WHEN 'IT' THEN 'Internal table' 
    WHEN 'K' THEN 'PRIMARY KEY or UNIQUE constraints' 
    WHEN 'L' THEN 'Logs' 
    WHEN 'P' THEN 'Stored procedures' 
    WHEN 'PC' THEN 'Assembly (CLR) stored-procedure' 
    WHEN 'PG' THEN 'Plan guide' 
    WHEN 'PK' THEN 'PRIMARY KEY constraint' 
    WHEN 'R' THEN 'Rules' 
    WHEN 'RF' THEN 'Replication filter stored procedures' 
    WHEN 'S' THEN 'System tables' 
    WHEN 'SN' THEN 'Synonym' 
    WHEN 'SO' THEN 'Sequence object' 
    WHEN 'SQ' THEN 'Service queue' 
    WHEN 'TF' THEN 'Table functions' 
    WHEN 'TR' THEN 'Triggers' 
    WHEN 'U' THEN 'User tables' 
    WHEN 'UQ' THEN 'UNIQUE constraint' 
    WHEN 'V' THEN 'Views' 
    WHEN 'X' THEN 'Extended stored procedures' 
    ELSE type
END 
FROM sys.objects 
GROUP BY type 
ORDER BY 'Count' desc

Use this script. It is not using switch case statement.

USE [MyDatabase]
GO
select distinct type_desc as 'Type Description', Count from
(SELECT 'Count' = COUNT(*), type FROM sys.objects GROUP BY type) as dbstatistics 
left join sys.objects on dbstatistics.type = sys.objects.type ORDER BY Count desc
GO
SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbName';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!