SQL SERVER 数据库查询物理表主键列名


SELECT t.name Tname,p.name Pname,c.name Cname FROM sys.objects T INNER JOIN sys.objects P 
ON t.object_id=p.parent_object_id AND t.type='U' AND p.type='PK'
INNER JOIN sys.SysColumns C ON c.id=t.object_id 
INNER JOIN sysindexes i ON i.name=p.name
INNER JOIN sysindexkeys k ON k.id=c.id AND k.colid=c.colid AND k.indid=i.indid

 


select 
a.name as FieldName, -- 字段名
a.isnullable, -- 是否可为空
--b.Value as FieldDesc, -- 字段说明
c.name as FieldType, -- 数据类型
COLUMNPROPERTY(a.id,a.name,'IsIdentity') as isidentity, --是否标识列
PK=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then 'true' else 'false' end --是否主键
from SysColumns a left JOIN
systypes c on a.xusertype=c.xusertype
where a.id=Object_Id('BSalesAccountMapping')

posted @ 2020-12-18 23:39  博客燕  阅读(227)  评论(0编辑  收藏  举报