sql查詢表的表名、主鍵名、主鍵列名



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')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM