已经知道数据库中某个表名,如何取得该表的主键字段呢???

youhwa 2002-01-04 03:44:11
...全文
121 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamxia 2002-01-05
  • 打赏
  • 举报
回复
sc
xiatianyun 2002-01-04
  • 打赏
  • 举报
回复
请问系统表的作用?
zhuzhichao 2002-01-04
  • 打赏
  • 举报
回复
如果想知道表的主键的名字.那很简单:
select name from sysobjects where parent_obj = object_id('TableName') and xtype = 'PK';

如果是要知道表的主键有哪些字段,那幺这样:

select
COLUMN_NAME = convert(sysname,c.name),
KEY_SEQ = convert(smallint,c1.colid),
PK_NAME = convert(sysname,i.name)
from
sysindexes i, syscolumns c, sysobjects o, syscolumns c1
where
o.id = object_id('TableName')
and o.id = c.id
and o.id = i.id
and (i.status & 0x800) = 0x800
and c.name = index_col (quotename('TableName'), i.indid, c1.colid)
and c1.colid <= i.keycnt /* create rows from 1 to keycnt */
and c1.id = object_id('TableName')
order by 2

401

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 非技术版
社区管理员
  • 非技术版社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧