Thursday, 21 June 2012

Find tables without primary key and the number of rows for SQL Server

USE [Database Name];
GO
SELECT    SCHEMA_NAME(schema_id) AS SchemaName
        ,name AS TableName
        FROM    sys.tables
        WHERE    OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') = 0
                -- is there a better way to fileter out system tables?
                and name not like 'sys%'
                and name not like 'MSmerge%'
                and name not like 'MSrepl%'
                and name not like 'dpyDeployment%'
                and name not in ('dbmaintain_scripts')
        ORDER BY SchemaName, TableName;
GO

SELECT    SCHEMA_NAME(t.schema_id) AS SchemaName
        ,st.row_count as NumberOfRows
        ,t.name AS TableName
        FROM    sys.tables t inner join sys.dm_db_partition_stats st
                on t.object_id = st.object_id
        WHERE    OBJECTPROPERTY(t.OBJECT_ID,'TableHasPrimaryKey') = 0
                -- is there a better way to fileter out system tables?
                and t.name not like 'sys%'
                and t.name not like 'MSmerge%'
                and t.name not like 'MSrepl%'
                and t.name not like 'dpyDeployment%'
                and t.name not like 'MSpeer%'
                and t.name not in ('dbmaintain_scripts', 'MSdynamicsnapshotjobs', 'MSpub_identity_range')
                --and st.row_count>100
                and st.index_id < 2
        ORDER BY SchemaName, NumberOfRows desc--TableName;
GO
 SELECT    OBJECT_NAME(OBJECT_ID) TableName
        , st.row_count as NumberOfRows
        FROM    sys.dm_db_partition_stats st
        WHERE    index_id < 2
GO



SELECT    SCHEMA_NAME(t.schema_id) AS SchemaName

        ,st.row_count as NumberOfRows

        ,t.name AS TableName

        FROM    sys.tables t inner join
                    -- we have partitions for tables
                    (
                    select  
                        object_id
                        , sum(row_count) as row_count
                        from sys.dm_db_partition_stats
                        where index_id < 2
                        group by object_id      
                        --order by object_id
                    ) st

                on t.object_id = st.object_id

        WHERE    OBJECTPROPERTY(t.OBJECT_ID,'TableHasPrimaryKey') = 0

                -- is there a better way to fileter out system tables?

                and t.name not like 'sys%'

                and t.name not like 'MSmerge%'

                and t.name not like 'MSrepl%'

                and t.name not like 'dpyDeployment%'

                and t.name not like 'MSpeer%'
                and t.name not like 'MSsavedfor%'

                and t.name not in ('dbmaintain_scripts', 'MSdynamicsnapshotjobs', 'MSpub_identity_range', 'MSsubscription_agents', 'MSsnapshotdeliveryprogress')

                --and st.row_count>100

                --and st.index_id < 2

                -- further check index

                and --t.name not in

                t.object_id not in

                    (

                    select distinct --OBJECT_NAME(i.id),

                        id

                        from sysindexes i

                        WHERE    (i.indid BETWEEN 1 AND 254)

                                -- leave out AUTO_STATISTICS:

                                AND (i.Status & 64)=0

                                -- leave out system tables:

                                AND OBJECTPROPERTY(i.id, 'IsMsShipped') = 0

                    )

        ORDER BY SchemaName, NumberOfRows desc--TableName;GO

No comments:

Post a Comment