只有主键表

我想找到只有我指定列的主键的表。我正在尝试的如下:只有主键表

SELECT 

OBJECT_NAME(ic.OBJECT_ID) AS TableName,

1 AS InsertPrimaryGuidOnly,

0 AS InsertCustomerOnly,

0 AS InsertApplicationOnly,

0 AS InsertNoKeyOnly

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 COL_NAME(ic.OBJECT_ID, ic.column_id) = 'CREDITCHECKGUID'

AND OBJECT_NAME(ic.OBJECT_ID) NOT IN ('Customer', 'Application')

我正在获取表列表,但它也列出了具有作为主键和外键列的下表。我想要的只是主键表。如果主服务器也是外键,我不想列出该表。请帮忙。

回答:

把这个where子句放到你的查询中;

AND ic.OBJECT_ID NOT IN (select parent_object_id from sys.foreign_keys fk) 

完整查询;

SELECT 

ic.OBJECT_ID,

OBJECT_NAME(ic.OBJECT_ID) AS TableName,

1 AS InsertPrimaryGuidOnly,

0 AS InsertCustomerOnly,

0 AS InsertApplicationOnly,

0 AS InsertNoKeyOnly

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 COL_NAME(ic.OBJECT_ID, ic.column_id) = 'CREDITCHECKGUID'

AND OBJECT_NAME(ic.OBJECT_ID) NOT IN ('Customer', 'Application')

AND ic.OBJECT_ID NOT IN (select parent_object_id from sys.foreign_keys fk)

以上是 只有主键表 的全部内容, 来源链接: utcz.com/qa/258353.html

回到顶部