10 个解决方案
#1
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
#2
以前有过类似的帖子
SELECT TableName=o.name,OWNER=USER_NAME(o.uid),TableDescription=ISNULL(ptb.value,N''), FieldId=c.colid,FieldName=c.name, FieldType=QUOTENAME(t.name) +CASE WHEN t.name IN (N'decimal',N'numeric') THEN N'('+CAST(c.prec as varchar)+N','+CAST(c.scale as varchar)+N')' WHEN t.name=N'float' OR t.name like N'%char' OR t.name like N'%binary' THEN N'('+CAST(c.prec as varchar)+N')' ELSE N'' END +CASE WHEN c.isnullable=1 THEN N'' ELSE N' NOT' END+N' NULL', FieldDescription=ISNULL(pfd.value,''), DefileLength=c.length, FieldDefault=ISNULL(df.text,N''), IsIDENTITY=COLUMNPROPERTY(o.id,c.name,N'IsIdentity'), IsComputed=COLUMNPROPERTY(o.id,c.name,N'IsComputed'), IsROWGUID=COLUMNPROPERTY(o.id,c.name,N'IsRowGuidCol'), IsPrimaryKey=CASE WHEN opk.xtype IS NULL THEN 0 ELSE 1 END FROM sysobjects o JOIN syscolumns c ON c.id=o.id AND OBJECTPROPERTY(o.id,N'IsUserTable')=1 JOIN systypes t ON t.xusertype=c.xusertype LEFT JOIN syscomments df ON df.id=c.cdefault LEFT JOIN sysproperties ptb ON ptb.id=o.id and ptb.smallid=0 LEFT JOIN sysproperties pfd ON pfd.id=o.id and pfd.smallid=c.colid LEFT JOIN sysindexkeys idxk ON idxk.id=o.id AND idxk.colid=c.colid LEFT JOIN sysindexes idx ON idx.indid=idxk.indid AND idx.id=idxk.id AND idx.indid NOT IN(0,255) LEFT JOIN sysobjects opk ON opk.parent_obj=o.id AND opk.name=idx.name AND OBJECTPROPERTY(opk.id,N'IsPrimaryKey')=1 ORDER BY o.name,c.colid
SELECT TableName=o.name,OWNER=USER_NAME(o.uid),TableDescription=ISNULL(ptb.value,N''), FieldId=c.colid,FieldName=c.name, FieldType=QUOTENAME(t.name) +CASE WHEN t.name IN (N'decimal',N'numeric') THEN N'('+CAST(c.prec as varchar)+N','+CAST(c.scale as varchar)+N')' WHEN t.name=N'float' OR t.name like N'%char' OR t.name like N'%binary' THEN N'('+CAST(c.prec as varchar)+N')' ELSE N'' END +CASE WHEN c.isnullable=1 THEN N'' ELSE N' NOT' END+N' NULL', FieldDescription=ISNULL(pfd.value,''), DefileLength=c.length, FieldDefault=ISNULL(df.text,N''), IsIDENTITY=COLUMNPROPERTY(o.id,c.name,N'IsIdentity'), IsComputed=COLUMNPROPERTY(o.id,c.name,N'IsComputed'), IsROWGUID=COLUMNPROPERTY(o.id,c.name,N'IsRowGuidCol'), IsPrimaryKey=CASE WHEN opk.xtype IS NULL THEN 0 ELSE 1 END FROM sysobjects o JOIN syscolumns c ON c.id=o.id AND OBJECTPROPERTY(o.id,N'IsUserTable')=1 JOIN systypes t ON t.xusertype=c.xusertype LEFT JOIN syscomments df ON df.id=c.cdefault LEFT JOIN sysproperties ptb ON ptb.id=o.id and ptb.smallid=0 LEFT JOIN sysproperties pfd ON pfd.id=o.id and pfd.smallid=c.colid LEFT JOIN sysindexkeys idxk ON idxk.id=o.id AND idxk.colid=c.colid LEFT JOIN sysindexes idx ON idx.indid=idxk.indid AND idx.id=idxk.id AND idx.indid NOT IN(0,255) LEFT JOIN sysobjects opk ON opk.parent_obj=o.id AND opk.name=idx.name AND OBJECTPROPERTY(opk.id,N'IsPrimaryKey')=1 ORDER BY o.name,c.colid
#3
这个只能在2000中执行,2005不可以
#4
谢谢楼上的回复,我发现上面的语句都可以在查询分析器中执行,现在我想把它做成视图在程序中调用,结果保存视图时sqlserver提示“ODBC 错误: [Microsoft][ODBC SQL Server Driver][SQL Server]除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。”如何解决呢?谢谢!
#5
去掉最后面的order by 或者最前面改为 select top 100 percent
#6
SELECT dbo.sysobjects.name AS [table]
,dbo.syscolumns.name AS columns
,dbo.sysproperties.value AS description
FROM dbo.sysobjects
INNER JOIN dbo.syscolumns ON dbo.sysobjects.id = dbo.syscolumns.id
LEFT JOIN dbo.sysproperties ON dbo.syscolumns.id = dbo.sysproperties.id AND dbo.syscolumns.colid = dbo.sysproperties.smallid
WHERE (dbo.sysobjects.xtype = 'u')
#7
我是楼主,谢谢大家回复,大家的语句在查询分析器里都可以正常运行,可是作为视图里运行,字段描述一列就得不到字段的中文描述,而是<Binary>,如何处理?谢谢!
#8
试试CAST(dbo.sysproperties.value AS NVARCHAR(1000)) as description
#9
关注中
#10
做成视图?做成存储过程吧,传入表名参数.
#1
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
#2
以前有过类似的帖子
SELECT TableName=o.name,OWNER=USER_NAME(o.uid),TableDescription=ISNULL(ptb.value,N''), FieldId=c.colid,FieldName=c.name, FieldType=QUOTENAME(t.name) +CASE WHEN t.name IN (N'decimal',N'numeric') THEN N'('+CAST(c.prec as varchar)+N','+CAST(c.scale as varchar)+N')' WHEN t.name=N'float' OR t.name like N'%char' OR t.name like N'%binary' THEN N'('+CAST(c.prec as varchar)+N')' ELSE N'' END +CASE WHEN c.isnullable=1 THEN N'' ELSE N' NOT' END+N' NULL', FieldDescription=ISNULL(pfd.value,''), DefileLength=c.length, FieldDefault=ISNULL(df.text,N''), IsIDENTITY=COLUMNPROPERTY(o.id,c.name,N'IsIdentity'), IsComputed=COLUMNPROPERTY(o.id,c.name,N'IsComputed'), IsROWGUID=COLUMNPROPERTY(o.id,c.name,N'IsRowGuidCol'), IsPrimaryKey=CASE WHEN opk.xtype IS NULL THEN 0 ELSE 1 END FROM sysobjects o JOIN syscolumns c ON c.id=o.id AND OBJECTPROPERTY(o.id,N'IsUserTable')=1 JOIN systypes t ON t.xusertype=c.xusertype LEFT JOIN syscomments df ON df.id=c.cdefault LEFT JOIN sysproperties ptb ON ptb.id=o.id and ptb.smallid=0 LEFT JOIN sysproperties pfd ON pfd.id=o.id and pfd.smallid=c.colid LEFT JOIN sysindexkeys idxk ON idxk.id=o.id AND idxk.colid=c.colid LEFT JOIN sysindexes idx ON idx.indid=idxk.indid AND idx.id=idxk.id AND idx.indid NOT IN(0,255) LEFT JOIN sysobjects opk ON opk.parent_obj=o.id AND opk.name=idx.name AND OBJECTPROPERTY(opk.id,N'IsPrimaryKey')=1 ORDER BY o.name,c.colid
SELECT TableName=o.name,OWNER=USER_NAME(o.uid),TableDescription=ISNULL(ptb.value,N''), FieldId=c.colid,FieldName=c.name, FieldType=QUOTENAME(t.name) +CASE WHEN t.name IN (N'decimal',N'numeric') THEN N'('+CAST(c.prec as varchar)+N','+CAST(c.scale as varchar)+N')' WHEN t.name=N'float' OR t.name like N'%char' OR t.name like N'%binary' THEN N'('+CAST(c.prec as varchar)+N')' ELSE N'' END +CASE WHEN c.isnullable=1 THEN N'' ELSE N' NOT' END+N' NULL', FieldDescription=ISNULL(pfd.value,''), DefileLength=c.length, FieldDefault=ISNULL(df.text,N''), IsIDENTITY=COLUMNPROPERTY(o.id,c.name,N'IsIdentity'), IsComputed=COLUMNPROPERTY(o.id,c.name,N'IsComputed'), IsROWGUID=COLUMNPROPERTY(o.id,c.name,N'IsRowGuidCol'), IsPrimaryKey=CASE WHEN opk.xtype IS NULL THEN 0 ELSE 1 END FROM sysobjects o JOIN syscolumns c ON c.id=o.id AND OBJECTPROPERTY(o.id,N'IsUserTable')=1 JOIN systypes t ON t.xusertype=c.xusertype LEFT JOIN syscomments df ON df.id=c.cdefault LEFT JOIN sysproperties ptb ON ptb.id=o.id and ptb.smallid=0 LEFT JOIN sysproperties pfd ON pfd.id=o.id and pfd.smallid=c.colid LEFT JOIN sysindexkeys idxk ON idxk.id=o.id AND idxk.colid=c.colid LEFT JOIN sysindexes idx ON idx.indid=idxk.indid AND idx.id=idxk.id AND idx.indid NOT IN(0,255) LEFT JOIN sysobjects opk ON opk.parent_obj=o.id AND opk.name=idx.name AND OBJECTPROPERTY(opk.id,N'IsPrimaryKey')=1 ORDER BY o.name,c.colid
#3
这个只能在2000中执行,2005不可以
#4
谢谢楼上的回复,我发现上面的语句都可以在查询分析器中执行,现在我想把它做成视图在程序中调用,结果保存视图时sqlserver提示“ODBC 错误: [Microsoft][ODBC SQL Server Driver][SQL Server]除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。”如何解决呢?谢谢!
#5
去掉最后面的order by 或者最前面改为 select top 100 percent
#6
SELECT dbo.sysobjects.name AS [table]
,dbo.syscolumns.name AS columns
,dbo.sysproperties.value AS description
FROM dbo.sysobjects
INNER JOIN dbo.syscolumns ON dbo.sysobjects.id = dbo.syscolumns.id
LEFT JOIN dbo.sysproperties ON dbo.syscolumns.id = dbo.sysproperties.id AND dbo.syscolumns.colid = dbo.sysproperties.smallid
WHERE (dbo.sysobjects.xtype = 'u')
#7
我是楼主,谢谢大家回复,大家的语句在查询分析器里都可以正常运行,可是作为视图里运行,字段描述一列就得不到字段的中文描述,而是<Binary>,如何处理?谢谢!
#8
试试CAST(dbo.sysproperties.value AS NVARCHAR(1000)) as description
#9
关注中
#10
做成视图?做成存储过程吧,传入表名参数.