Donnerstag, 5. Januar 2012

Get Tables without any unique key column(s)

As i posted a SQL Server 2000 compatible version of this task in May (link), so I want to add today a version, that is using the new system tables of SQL Server 2005+.

 

USE YourDataBase;
GO

SELECT
 ss.name SchemaName
,so.name TableName
FROM sys.objects so
INNER JOIN sys.schemas ss ON
    so.schema_id = ss.schema_id
WHERE
    so.type = 'U' /* only select user tables */
AND NOT EXISTS (
            SELECT 1
            FROM sys.indexes si
            WHERE si.object_id = so.object_id AND type_desc = 'CLUSTERED'
           
            UNION
           
            SELECT 1
            FROM sys.indexes si
            WHERE si.object_id = so.object_id AND (is_unique_constraint = 1 OR is_unique = 1)
            )

;

Keine Kommentare:

Kommentar veröffentlichen