因為用SQL建立資料表時,PK_AgrRRRF01發生重複,逼迫我將PK_AgrRRRF01改成PK_AgrRRRF01_2,雖然資料表建立成功,可是我仍然心有不甘,想要知道是那個資料表佔用這個名字,於是我下了以下指令搜尋:
SELECT name, object_id, OBJECT_NAME(object_id) AS object_name, parent_object_id, OBJECT_NAME(parent_object_id) AS table_name
FROM sys.objects
WHERE type = 'PK' AND name LIKE '%PK_AgrRRRF01%'
ORDER BY name ASC
其中OBJECT_NAME()傳回結構描述範圍物件的資料庫物件名稱,name是物件名稱,object_id是物件識別碼,parent_object_id是這個物件所屬的物件識別碼(0 = 不是子物件)。
參考資料
sys.objects (Transact-SQL)