--SQL2005
select
[表名]=c.Name,
[表说明]=isnull(f.[value],''),
[列名]=a.Name,
[列序号]=a.Column_id,
[标识]=case when is_identity=1 then '√' else '' end,
[主键]=case when exists(select 1 from sys.objects x join sys.indexes y on x.Type=N'PK' and x.Name=y.Name
join sysindexkeys z on z.ID=a.Object_id and z.indid=y.index_id and z.indid in (0,1) and z.Colid=a.Column_id)
then '√' else '' end,
[类型]=b.Name,
[字节数]=case when a.[max_length]=-1 and b.Name!='xml' then 'max/2G'
when b.Name='xml' then '2^31-1字节/2G'
else rtrim(a.[max_length]) end,
[长度]=case when ColumnProperty(a.object_id,a.Name,'Precision')=-1 then '2^31-1'
else rtrim(ColumnProperty(a.object_id,a.Name,'Precision')) end,
[小数]=isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0),
[是否为空]=case when a.is_nullable=1 then '√' else '' end,
[列说明]=isnull(e.[value],''),
[默认值]=isnull(d.text,'')
from
sys.columns a
left join
sys.types b on a.user_type_id=b.user_type_id
inner join
sys.objects c on a.object_id=c.object_id and c.Type='U'
left join
sys.syscomments d on a.default_object_id=d.ID
left join
sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.Column_id and e.class=1
left join
sys.extended_properties f on f.major_id=c.object_id and f.minor_id=0 and f.class=1
--where c.Name='Tester'
select
[表名]=c.Name,
[表说明]=isnull(f.[value],''),
[列名]=a.Name,
[列序号]=a.Column_id,
[标识]=case when is_identity=1 then '√' else '' end,
[主键]=case when exists(select 1 from sys.objects x join sys.indexes y on x.Type=N'PK' and x.Name=y.Name
join sysindexkeys z on z.ID=a.Object_id and z.indid=y.index_id and z.indid in (0,1) and z.Colid=a.Column_id)
then '√' else '' end,
[类型]=b.Name,
[字节数]=case when a.[max_length]=-1 and b.Name!='xml' then 'max/2G'
when b.Name='xml' then '2^31-1字节/2G'
else rtrim(a.[max_length]) end,
[长度]=case when ColumnProperty(a.object_id,a.Name,'Precision')=-1 then '2^31-1'
else rtrim(ColumnProperty(a.object_id,a.Name,'Precision')) end,
[小数]=isnull(ColumnProperty(a.object_id,a.Name,'Scale'),0),
[是否为空]=case when a.is_nullable=1 then '√' else '' end,
[列说明]=isnull(e.[value],''),
[默认值]=isnull(d.text,'')
from
sys.columns a
left join
sys.types b on a.user_type_id=b.user_type_id
inner join
sys.objects c on a.object_id=c.object_id and c.Type='U'
left join
sys.syscomments d on a.default_object_id=d.ID
left join
sys.extended_properties e on e.major_id=c.object_id and e.minor_id=a.Column_id and e.class=1
left join
sys.extended_properties f on f.major_id=c.object_id and f.minor_id=0 and f.class=1
--where c.Name='Tester'
go
SELECT CASE WHEN a.colorder = 1 THEN d .name ELSE '' END AS 表名,
CASE WHEN a.colorder = 1 THEN isnull(CONVERT(nvarchar, f.value), '')
ELSE '' END AS 表说明, a.colorder AS 字段序号, a.name AS 字段名,
CASE WHEN COLUMNPROPERTY(a.id, a.name, 'IsIdentity')
= 1 THEN '√' ELSE '' END AS 标识, CASE WHEN EXISTS
(SELECT 1
FROM sysobjects
WHERE parent_obj = d.id AND xtype = 'PK' 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 AS 主键, b.name AS 类型, a.length AS 占用字节数,
COLUMNPROPERTY(a.id, a.name, 'PRECISION') AS 长度,
ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数,
CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空, ISNULL(e.text, '')
AS 默认值, ISNULL(CONVERT(nvarchar, g.[value]), '') AS 字段说明
FROM syscolumns a LEFT OUTER 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 OUTER JOIN
syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
sysproperties g ON a.id = g.id AND a.colid = g.smallid LEFT OUTER JOIN
sysproperties f ON d .id = f.id AND f.smallid = 0
ORDER BY d .name, a.colorder
我的这段是哪出错了呢?
SELECT colName=a.name,
ISKey=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name
in (SELECT name FROM sysindexes WHERE indid
in (SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid)))
then '1' else '' end
FROM syscolumns a inner join sysobjects d on a.id=d.id
left join syscomments e on a.cdefault=e.id
where d.name = 't_mo3d'
谢谢了。
SELECT colName = a.name, ISKey = CASE WHEN EXISTS
(SELECT 1
FROM sysobjects
WHERE parent_obj = d .id AND xtype = 'PK' AND name IN
(SELECT name
FROM sysindexes
WHERE indid IN
(SELECT indid
FROM sysindexkeys
WHERE id = a.id AND colid = a.colid)))
THEN '1' ELSE '' END
FROM syscolumns a INNER JOIN
sysobjects d ON a.id = d .id LEFT JOIN
syscomments e ON a.cdefault = e.id
WHERE d .name = 't_mo3d'