SQL中 怎样获得一个表的字段名及其属性?

jb2008 2003-05-08 10:59:40
如题
...全文
116 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
8312008 2003-05-08
楼主问题所涉及的系统表如下:
(1)sysobjects
保留表名(name),表类型(xtype='u'用户表或存储过程),id(系统为每个表分配的不同ID号)
(2)syscolumns
保留列名(name),所属表或存储过程的ID号(id),字段类型(xtype对应于systypes中的字段类型),字段长度(length)
(3)systypes
保留数据类型名称(name),字段类型(xtype,syscolumns中xtype与此对应)

语句楼上各位都已写出,就不重复了
回复
wgy2008 2003-05-08
SELECT
(case when a.colorder=1 then d.name else '' end)表名,
a.colorder 字段序号,
a.name 字段名,
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) 标识,
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) 主键,
b.name 类型,
a.length 占用字节数,
COLUMNPROPERTY(a.id,a.name,'PRECISION') as 长度,
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as 小数位数,
(case when a.isnullable=1 then '√'else '' end) 允许空,
isnull(e.text,'') 默认值,
isnull(g.[value],'') AS 字段说明

FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sysproperties g
on a.id=g.id AND a.colid = g.smallid
order by a.id,a.colorder
回复
coffee_cn 2003-05-08
有空的话你可以去研究研究数据库中以sys开头的一些表
回复
happydreamer 2003-05-08
select A.name as col, C.name as type, A.length from
syscolumns A left join sysobjects B
on A.id = B.id left join systypes C
on A.xtype = C.xtype
where B.name = 'tablename'
group by A.name, C.name, A.length
回复
yoki 2003-05-08
select a.colid as ID,a.name as 列名,b.name as 类型,a.length as 长度,
case a.isnullable when 1 then '是' else '否' end as 允许空
from syscolumns a,systypes b
where a.id=object_id('你的表名') and a.xtype=b.xtype
回复
CrazyFor 2003-05-08
SELECT
(case when a.colorder=1 then d.name else '' end) N'表名',
a.colorder N'字段序号',
a.name N'字段名',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'标识',
(case when (SELECT count(*)
FROM sysobjects
WHERE (name in
(SELECT name
FROM sysindexes
WHERE (id = a.id) AND (indid in
(SELECT indid
FROM sysindexkeys
WHERE (id = a.id) AND (colid in
(SELECT colid
FROM syscolumns
WHERE (id = a.id) AND (name = a.name))))))) AND
(xtype = 'PK'))>0 then '√' else '' end) N'主键',
b.name N'类型',
a.length N'占用字节数',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'长度',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'小数位数',
(case when a.isnullable=1 then '√'else '' end) N'允许空',
isnull(e.text,'') N'默认值',
isnull(g.[value],'') AS N'字段说明'
--into ##tx

FROM syscolumns a left join systypes b
on a.xtype=b.xusertype
inner join sysobjects d
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e
on a.cdefault=e.id
left join sysproperties g
on a.id=g.id AND a.colid = g.smallid
order by object_name(a.id),a.colorder
回复
jb2008 2003-05-08
谢谢大家
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-05-08 10:59
社区公告
暂无公告