日期:2014-05-18 浏览次数:20723 次
--查询用户表对象信息
select Tab.Name as [表名],Tab.create_date as [创建时间],Tab.modify_date as [最后修改时间],
Col.Name as [列名] ,Type.name as [数据类型],Col.max_length as [字段长度],
CASE WHEN pk.is_primary_key= 1 THEN 'Y' ELSE 'N' end as [是否主键],
CASE WHEN Col.is_identity = 1 THEN 'Y' else 'N'end as [是否自增] ,
identity_columns.seed_value as [自增种子],identity_columns.increment_value as [自增步长],
case when Col.is_nullable = 1 then 'Y' else 'N' END AS [是否允许为NULL],
Def.text as [默认值],case when Col.is_computed = 1 then 'Y' else 'N' END as [是否计算列] ,
computed_columns.definition as [计算公式],Col_Desc.Value as [列备注]
from sys.objects Tab inner join sys.columns Col on Tab.object_id =Col.object_id
inner join sys.types Type on Col.system_type_id = Type.system_type_id
left join sys.identity_columns identity_columns on Tab.object_id = identity_columns.object_id and Col.column_id = identity_columns.column_id
left join syscomments Def on Col.default_object_id = Def.ID
left join(
select index_columns.object_id,index_columns.column_id,indexes.is_primary_key
from sys.indexes indexes inner join sys.index_columns index_columns
on indexes.object_id = index_columns.object_id and indexes.index_id = index_columns.index_id
where indexes.is_primary_key = 1/*主键*/
) PK on Tab.object_id = PK.object_id AND Col.column_id = PK.column_id
left join sys.computed_columns computed_columns on Tab.object_id =computed_columns.object_id and Col.column_id = computed_columns.column_id
left join sys.extended_properties Col_Desc on Col_Desc.major_id=Tab.object_id and Col_Desc.minor_id=Col.Column_id and Col_Desc.class=1
where Tab.type = 'U' and Tab.Name not like'sys%'
order by Tab.create_date
------解决方案--------------------
SELECT t.[name] AS [表名],c.[name] AS [字段名],cast(ep.[value] as varchar(100)) AS [字段说明] FROM sys.tables AS t INNER JOIN sys.columns AS c ON t.object_id = c.object_id LEFT JOIN sys.extended_properties AS ep ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
------解决方案--------------------
--sql server 2000
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表说明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号 = a.colorder,
字段名 = a.name,
标识 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键 = 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(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空 = case when a.isnullable=1 then '√'else '' end,
默认值 = 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='要查询的表' --如果只查询指定表,加上此条件
order by
a.id,a.colorder
--sql server 2005
-- 1. 表结构信息查询
-- ========================================================================
-- 表结构信息查询
-- 邹建 2005.08(引用请保留此信息)
-- ========================================================