SQL server根據表名查詢表主鍵

cjc發表於2021-10-13

SQL server根據表名查詢表主鍵

SELECT o.name AS 表名
    ,c.name AS 主鍵列名
    ,k.colid AS 欄位序號
    ,k.keyno AS 索引順序
FROM sysindexes i
INNER JOIN sysindexkeys k ON i.id = k.id
    AND i.indid = k.indid
INNER JOIN sysobjects o ON i.id = o.id
INNER JOIN syscolumns c ON i.id = c.id
    AND k.colid = c.colid
WHERE o.xtype = 'U'
    AND EXISTS (
        SELECT 1
        FROM sysobjects
        WHERE xtype = 'PK'
            AND name = i.name
        )
    AND o.name = 'TJ_MZYW' --表名稱
ORDER BY o.name
    ,k.colid
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章