29

Test1在 SQL Server 2008 R2 中有数据库。在实时服务器上,我从那里备份并在我们的本地机器上恢复它,Test2并添加了一些表和程序。

如果我们恢复Test2到实时服务器,那么是否有任何查询可以获取仅在测试 2 中而不在测试 1 中的表名称和过程名称,或者 SQL Server 将其视为完全不同的数据库?

如果我只想知道Test1Test2 数据库的差异数量,查询是什么

4

7 回答 7

84

这将为您提供表和存储过程的计数。

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

您可以在sys.objects数据库中找到所有类型的对象。您必须在每个数据库上运行此查询才能查看对象数。

sys.objects 您可以在此处找到有关所存储内容的所有信息。

于 2013-10-11T09:11:53.913 回答
16

您可以使用这两个查询:

select count(*) as TablesCount from sys.tables
select count(*) as ProceduresCount from sys.procedures
于 2013-10-11T09:14:25.037 回答
7

我经常使用我在这个博客上找到的这个脚本

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

您可以从有关sys.objects的信息中按类型对其进行修改

或通过此参考Object Catalog Views中的对象,因为您已经在前面的答案中得到了表和过程的答案,例如

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

希望这能给你一些额外的帮助

于 2014-01-23T09:09:55.983 回答
4

我现在使用以下内容,基于 Milica 的回答,带有一些额外的类型、默认值并按计数排序。

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
于 2016-11-24T10:28:19.103 回答
2

使用以下查询。

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

select Count(*) AS TotalProc from sys.procedures
于 2016-05-20T18:28:58.580 回答
2

使用这个脚本。它没有使用 switch case 语句。

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
于 2017-05-02T13:07:08.650 回答
-1
SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'dbName';
于 2014-04-11T05:30:42.430 回答