查询数据库表结构

查询数据库表结构
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,
字段说明 = isnull(G.[value],’’),
标识 = Case When COLUMNPROPERTY( A.id,A.name,’IsIdentity’)=1 Then N’是’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 N’是’ 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 N’’ End,—√
默认值 = isnull(E.Text,’’)
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
sys.extended_properties G
on
A.id=G.major_id and A.colid=G.minor_id
Left Join
sys.extended_properties F
On
D.id=F.major_id and F.minor_id=0
—where d.name=’OrderInfo’ —如果只查询指定表,加上此条件
Order By
A.id,A.colorder


select
[表名]=c.Name,
[表说明]=isnull(f.[value],’’),
[列序号]=a.Column_id,
[列名]=a.Name,
[列说明]=isnull(e.[value],’’),
[数据库类型]=b.Name,
[类型]= case when b.Name = ‘image’ then ‘byte[]’
when b.Name in(‘image’,’uniqueidentifier’,’ntext’,’varchar’,’ntext’,’nchar’,’nvarchar’,’text’,’char’) then ‘string’
when b.Name in(‘tinyint’,’smallint’,’int’,’bigint’) then ‘int’
when b.Name in(‘date’,’datetime’,’smalldatetime’) then ‘DateTime’
when b.Name in(‘float’,’decimal’,’numeric’,’money’,’real’,’smallmoney’) then ‘decimal’
when b.Name =’bit’ then ‘bool’ else b.name end ,
[标识]= 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.Colid=a.Column_id)
then ‘是’ else ‘’ end,
[字节数]=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(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
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 1 = 1 —and c.Name=’Users’ —如果只查询指定表,加上此条件


—得到所有表查询语句
SELECT ‘SELECT * FROM ‘+sys.sysobjects.name FROM sys.sysobjects LEFT JOIN sys.extended_properties ON sys.sysobjects.id=sys.extended_properties.major_id AND sys.extended_properties.minor_id=0 WHERE sys.sysobjects.xtype=’U’


select
[表名]=c.Name,
[表说明]=isnull(f.[value],’’),
[列序号]=a.Column_id,
[列名]=a.Name,
[数据库类型]=CASE WHEN b.Name=’varchar’THEN
CAST(b.Name AS VARCHAR(200))+’(‘+
CAST((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) AS VARCHAR(200))—字节数
+’)’
WHEN b.Name=’numeric’ OR b.Name=’decimal’ THEN
CAST(b.Name AS VARCHAR(200))+’(‘+
CAST((case when ColumnProperty(a.object_id,a.Name,’Precision’)=-1 then ‘2^31-1’
else rtrim(ColumnProperty(a.object_id,a.Name,’Precision’)) end) AS VARCHAR(200))—长度
+’, ‘+
CAST((isnull(ColumnProperty(a.object_id,a.Name,’Scale’),0)) AS VARCHAR(200))+’)’—小数位
ELSE b.Name END+’,’,
[列说明]=isnull(‘—‘+CAST(e.[value] AS VARCHAR(200)),’’),
[类型]= case when b.Name = ‘image’ then ‘byte[]’
when b.Name in(‘image’,’uniqueidentifier’,’ntext’,’varchar’,’ntext’,’nchar’,’nvarchar’,’text’,’char’) then ‘string’
when b.Name in(‘tinyint’,’smallint’,’int’,’bigint’) then ‘int’
when b.Name in(‘date’,’datetime’,’smalldatetime’) then ‘DateTime’
when b.Name in(‘float’,’decimal’,’numeric’,’money’,’real’,’smallmoney’) then ‘decimal’
when b.Name =’bit’ then ‘bool’ else b.name end ,
[标识]= 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.Colid=a.Column_id)
then ‘是’ else ‘’ end,
[字节数]=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(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
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 1 = 1 —and c.Name=’Orders’ —如果只查询指定表,加上此条件