我要確保這是獲得PK的名稱,架構,表名和列名指定表的名稱得到一個表的主鍵列
SELECT i.name AS IndexName,
(SELECT SCHEMA_NAME([schema_id]) FROM sys.[tables] WHERE [object_id] = ic.OBJECT_ID) SchemaName,
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
AND OBJECT_NAME(ic.OBJECT_ID) = 'MyTable'
的正確的方式,我相信這是工作
SELECT i.name AS IndexName,
SCHEMA_NAME([schema_id]) SchemaName,
OBJECT_NAME(ic.OBJECT_ID) AS TableName,
COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName,
ic.key_ordinal
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
INNER JOIN sys.[tables] st
ON st.[object_id] = ic.OBJECT_ID
WHERE i.is_primary_key = 1
AND SCHEMA_NAME([schema_id]) = 'auth'
AND OBJECT_NAME(ic.OBJECT_ID) = 'Profile'
似乎對我好,爲什麼問?你有什麼問題嗎? –