SQL: 查询主键

MSSQL SERVER 2016,  查询用户表中主键

SELECT obj.name  AS [TABLE_NAME],
       obj.object_id AS [TABLE_OBJECT_ID],
       COL.NAME AS COLUMN_NAME ,
	   PKCOL.NAME AS KEY_COLUMN_NAME,  -- 主键 
	   PK.*
FROM -- [sys].[key_constraints]  kc,
     sys.all_objects obj
     inner join sys.all_columns col on obj.object_id = col.object_id
     left outer join [sys].[sysindexkeys]  PK   -- 主键
        on col.object_id = pk.id  and col.column_id = pk.COLID  AND PK.INDID= 1  
     LEFT OUTER JOIN  sys.all_columns PKCOL ON PKCOL.column_id = PK.colid AND PKCOL.OBJECT_ID = PK.ID 
where  1=1  -- kc.parent_object_id = obj.object_id
  and obj.name like '%'
  and obj.type='U' 
  AND OBJ.object_id >=10000

  

posted @ 2023-07-22 16:29  samrv  阅读(312)  评论(0编辑  收藏  举报