[SQL]OBJECTPROPERTY Function
在SQL中如果我們要檢查資料庫各物件的屬性,可以透過OBJECTPROPERTY這個FUNCTION來判斷該屬性是否存在。
如果我們要檢查資料庫中那些TABLE沒有建立Primary Key,就可以用 OBJECTPROPERTY(table_object_id, 'TableHasPrimaryKey') 來判斷!
SELECT * FROM sys.tables
WHERE OBJECTPROPERTY(object_id, 'TableHasPrimaryKey') = 0;
另外,要找到DataBase中各Table的PK,可透過sys.indexes的is_primary_key = 1 來判斷(參考自:SQL SERVER – 2005 – Find Tables With Primary Key Constraint in Database)。
SELECT i.name AS IndexName,
OBJECT_NAME(ic.object_id) AS TableName,
COL_NAME(ic.object_id,ic.column_id) AS ColumnName
FROM sys.indexes AS i INNER JOIN
sys.index_columns AS ic ON i.object_id = ic.object_id
AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1;
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^