2013-02-26 29 views

回答

2

事情是這樣的:

SELECT 
      so.name AS TableName, 
      si.name AS IndexName, 
      si.type_desc AS IndexType, 
      si.is_primary_key 
FROM 
      sys.indexes si 
      JOIN sys.tables so ON si.[object_id] = so.[object_id] 
WHERE 
      si.type IN (0, 2) 
      AND si.is_primary_key=1 
ORDER BY 
      so.name 
+0

或更好:從SQL Server ** 2005 **開始,使用更具體的'sys.tables' - 節省您不必爲表格指定'type ='U'' ..... – 2013-02-26 15:56:28

+1

@ marc_s:我更新了答案。這是你的意思嗎? – Arion 2013-02-26 16:09:35

+0

好多了!另外:我認爲它應該是'si.type IN(0,2)' - 「0」是「堆」,「2」是其他非聚集索引。 – 2013-02-26 16:16:09

1
select O.name 
from sys.objects as O 
    inner join sys.indexes as I1 
    on O.object_id = I1.object_id 
    inner join sys.indexes as I2 
    on O.object_id = I2.object_id 
where O.type = 'U' and   -- U = Table (user-defined) 
     I1.type = 0 and   -- 0 = Heap 
     I2.is_primary_key = 1 

sys.objects (Transact-SQL)
sys.indexes (Transact-SQL)