2000
整理的
SELECT
表名=d.name,
表说明=isnull(f.value,''),
所有者=USER_NAME(d.uid),
字段序号=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 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,
字段类型=QUOTENAME(b.name) + CASE WHEN b.name IN (N'decimal',N'numeric') THEN N' ('+CAST(a.prec as varchar)+N',' + CAST(a.scale as varchar)+N')' WHEN b.name=N'float' OR b.name like N'%char' OR b.name like N'%binary' THEN N' ('+CAST(a.prec as varchar)+N')' ELSE N'' END,
占用字节数=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],''),
创建时间=d.crdate,
索引=isnull(x.name,''),
索引类型=isnull(x.xtype,'')
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
left join sysproperties f on d.id=f.id and f.smallid=0
left join (
SELECT x1.name, x3.xtype, x2.id, x2.colid, x3.parent_obj FROM sysindexes x1
inner join (select id, indid, max(colid) colid from sysindexkeys group by id,indid) x2 on x1.indid = x2.indid
inner join sysobjects x3 on x1.name = x3.name
) x on x.id = a.id and x.colid = a.colid and x.parent_obj = a.id
where d.name='tablename' --替换成自己的表名
Order by a.id,a.colorder
其他收集
SELECT
字段序号=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 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='TestTable' --如果只查询指定表,加上此条件
order by a.id,a.colorder
网络收集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 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],''),
创建时间=d.crdate
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
left join sysproperties f on d.id=f.id and f.smallid=0
where d.name='TableName' --替换成自己的表名
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,
FieldConstraint=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 '' ELSE 'Primary Key' 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
where o.name = 'TableName' --替换成自己的表名
ORDER BY o.name,c.colid
其他的功能
select 'public string '+t2.name+' { get; set; }', t2.length from sysobjects t1
inner join syscolumns t2 on t1.id=t2.id
where t1.name ='TableName' --替换成自己的表名
order by t2.colorder