MSSQL查询表名、字段、备注、类型、默认值等信息

格式一

SELECT c.name '字段名', TYPE_NAME(c.system_type_id)+ '(' + CONVERT(VARCHAR(20),c.max_length)  + ')' '字段类型',
CASE c.is_nullable WHEN 0 THEN '否' ELSE '是' END '是否允许为空',convert(varchar(100), isnull(cmts.text,'')) '默认值',p.value '备注' 
FROM 
sys.objects obj
JOIN sys.columns c ON c.object_id = obj.object_id
LEFT JOIN sys.extended_properties p ON c.object_id = p.major_id AND p.minor_id=c.column_id
LEFT JOIN sys.syscomments cmts ON c.default_object_id = cmts.id
WHERE 
obj.name='CustomerProductPool'

格式二

SELECT DISTINCT
	d.name,
	f.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 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


	SELECT  
        a.colorder as colOrder, 
        d.name as tableName,
        a.name as sqlName, 
        convert(int,case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then 1 else 0 end) as isIndex, 
        convert(int,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 1 else 0 end) as isPk, 
        b.name as sqlTypeName, 
        a.length as 'length', 
        convert(int,COLUMNPROPERTY(a.id,a.name,'PRECISION')) as 'size', 
        convert(int,isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0)) as 'scale', 
        convert(int,case when a.isnullable=1 then 1 else 0 end) as 'nullable', 
        convert(varchar(100), isnull(e.text,'')) as defaultVal, 
        convert(varchar(255), isnull(g.[value],'')) as remarks 
 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  1=1
        -- d.name=?  
 order by  
        a.id,a.colorder

你可能感兴趣的:(SQL)