SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段名 = a.name,
主键 = 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 '√' else '' end,
类型 = b.name,
占用字节数 = a.length,
长度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
默认值 = isnull(e.text,''),
字段说明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=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
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='LW_Equipment_Stock_List' --如果只查询指定表,加上此条件
Select Name From SysColumns Where ID=OBJECT_ID('table1') Order By ColID
下边是关于 Sysobjects的介绍
经常我们要查询表的索引,约束,相关性,触发器的属性,那么要知道sysobjects这个表的字段的意思,那么不管要查什么都没有问题!
Sysobjects:SQL-SERVER的每个数据库内都有此系统表,它存放该数据库内创建的所有对象,如约束、默认值、日志、规则、存储过程等,每个对象在表中占一行。以下是此系统表的字段名称和相关说明。
Name,id,xtype,uid,status:分别是对象名,对象ID,对象类型,所有者对象的用户ID,对象状态。
对象类型(xtype)。可以是下列对象类型中的一种:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
L = 日志
FN = 标量函数
IF = 内嵌表函数
P = 存储过程
PK = PRIMARY KEY 约束(类型是 K)
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
UQ = UNIQUE 约束(类型是 K)
V = 视图
X = 扩展存储过程
当xtype='U' and status>0代表是用户建立的表,对象名就是表名,对象ID就是表的ID值。
用: select * from misa.dbo.sysobjects where xtype='U'and status>0 就可以列出库misa中所有的用户建立的表名。
SELECT * FROM SYSOBJECTS WHERE PARENT_OBJ = OBJECT_ID( 'CS') AND XTYPE='TR'
列出表cs的所有属性,上面是trigger!
版本二:
SELECT
(case when a.colorder=1 then d.name else '' end) N'Table Name',
a.colorder N'Column SQ',
a.name N'Column Name',
(case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end) N'Idnetity FG',
(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'PK',
b.name N'Type',
a.length N'Bit Length',
COLUMNPROPERTY(a.id,a.name,'PRECISION') as N'Length',
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0) as N'DataScale',
(case when a.isnullable=1 then '√'else '' end) N'Null',
isnull(e.text,'') N'Default'
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
order by object_name(a.id),a.colorder
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wren2004/archive/2008/12/10/3489483.aspx