1.通过创建一个函数来获取表字段信息
create function [dbo].[F_TableViewDescription] ( @tableName nvarchar(100)--表名 ) returns table return( select TableName=(case when b.colorder=1 then a.name else '' end), TableMemo=( case when b.colorder=1 then c.value else '' end),b.colid as indexs,b.name as ColumnName, IsIdentity=case when COLUMNPROPERTY( b.id,b.name,'IsIdentity')=1 then '√'else '' end, IsPrimaryKey=case when exists(select 1 from where xtype='PK' and parent_obj=b.id and name in( select name from where indid in(select indid from where id=b.id and colid=b.colid))) then '√' else '' end, TypeName=d.name, Bytelength=( case when b.length=-1 then 'max' else Convert(varchar(50),b.length) end), ColumnLength=( case when COLUMNPROPERTY;Precision')=-1 then 'max' else CONVERT(varchar(50),COLUMNPROPERTY;Precision')) end), IsNull=case when COLUMNPROPERTY;AllowsNull')=1 then '√' else '' end, Scale=Convert(varchar(20),isnull(COLUMNPROPERTY;Scale'),0)), DefaultValue=e.text, ColumnMemo=isnull;') from a inner join b on a.id=b.id and a.xtype='U' and a.name<>'dtproperties' left join c on a.id=c.major_id and c.minor_id=0 left join d on b.xusertype=d.xusertype left join e on e.id=b.cdefault left join f on b.id=f.major_id and b.colid=f.minor_id where a.name=ltrim(rtrim(isnull(@tableName,''))) --order by b.colid )2.使用如下所示
select * from F_TableViewDescription('表名')