我已经得到了以下查询,它可以正常工作,并为我提供了我想要的结果。然而,返回结果需要3秒钟,我发现它太慢了。
任何人都知道更好的方法吗?
SELECT
@PrimaryKey = ccu.Column_Name
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu on
tc.CONSTRAINT_NAME = ccu.Constraint_name
WHERE
tc.CONSTRAINT_TYPE = 'Primary Key' and
tc.Table_Name = @TableName
--tc.Table_Schema = 'LookUp'
答案 0 :(得分:3)
我很惊讶您的查询需要很长时间,但您可以通过SQL Server目录视图获取相同的信息:
SELECT c.name
FROM sys.indexes AS i
JOIN sys.index_columns AS ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
JOIN sys.columns AS c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE
i.is_primary_key = 1
AND i.object_id = OBJECT_ID(N'dbo.YourTable', 'U');