ITPub博客

首页 > 数据库 > 数据库开发技术 > 显示数据库中表的主键

显示数据库中表的主键

原创 数据库开发技术 作者:bidwhome 时间:2007-02-13 18:54:09 0 删除 编辑
显示数据库中表的主键[@more@]

以下以sqlserver为例写的显示数据库中表的主键

declare @objname nvarchar(50),@i int, @thiskey nvarchar(131),@keys nvarchar(2126),@indid int,@objid int
CREATE TABLE #pktab
(
table_name nvarchar(50),
pk_name nvarchar(300)
)

declare ms_crs_ind cursor local static for
select a.name
from sysobjects a where xtype='U' and name like 'T%' order by name
open ms_crs_ind
fetch ms_crs_ind into @objname
while @@fetch_status >= 0
begin
set @objid=object_id(@objname)
set @indid=(select i.index_id
from sys.indexes i join sys.stats s
on i.object_id = s.object_id and i.index_id = s.stats_id
where i.object_id =@objid and i.is_primary_key<>0)

select @keys = index_col(@objname, @indid, 1), @i = 2
if (indexkey_property(@objid, @indid, 1, 'isdescending') = 1)
select @keys = @keys + '(-)'

select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'

while (@thiskey is not null )
begin
select @keys = @keys + ', ' + @thiskey, @i = @i + 1
select @thiskey = index_col(@objname, @indid, @i)
if ((@thiskey is not null) and (indexkey_property(@objid, @indid, @i, 'isdescending') = 1))
select @thiskey = @thiskey + '(-)'
end
insert #pktab select @objname as tablename,@keys as pk_name
fetch ms_crs_ind into @objname
end
select * from #pktab
drop table #pktab

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7600305/viewspace-899455/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: SSAS中Cube的结构
请登录后发表评论 登录
全部评论

注册时间:2009-11-20

  • 博文量
    61
  • 访问量
    1035670