檢視SQLSERVER主鍵列

zhouwf0726發表於2019-03-31
檢視SQLSERVER主鍵列

SELECT
sysobjects.name,syscons.name,syscolumns.name
From
sysobjects
inner join syscolumns
on sysobjects.id = syscolumns.id
left outer join
(select
o.name sTableName,
c.Name sColName
From
sysobjects o
inner join sysindexes i
on o.id = i.id
and (i.status & 0X800) = 0X800
inner join syscolumns c1
on c1.colid <= i.keycnt
and c1.id = o.id
inner join syscolumns c
on o.id = c.id
and c.name = index_col (o.name, i.indid, c1.colid)) pkElements
on pkElements.sTableName = sysobjects.name
and pkElements.sColName = syscolumns.name
inner join sysobjects syscons on sysobjects.id=syscons.parent_obj and syscons.xtype='PK'
where
sysobjects.name in
('TAB1','TAB2'......) and sTableName is not null
order by
--Case (syscolumns.Status & 128) when 128 then 1 else 0 end DESC,
sysobjects.name

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/756652/viewspace-242472/,如需轉載,請註明出處,否則將追究法律責任。

相關文章