sqlserver数据库及表信息查询

  • 查询sqlserver所有的数据库
select * from sysdatabases;
  • 查询sqlserver数据库下有哪些表和视图
SELECT name FROM SysObjects where xtype='U' or xtype='V' ORDER BY Name;   --所有表的话是U  视图下的表的话是V
  • 查询sqlserver表有哪些字段,字段类型
select name from syscolumns where id=Object_Id('表名');   --查询有哪些字段

select sc.name,st.name from syscolumns sc,systypes st where sc.xtype=st.xtype and sc.id in(select id from sysobjects where xtype='U' and name='表名'); --查询字段类型


--按原表字段顺序排列的话
select sc.name,st.name from syscolumns sc,systypes st where sc.xtype=st.xtype and sc.id in(select id from sysobjects where xtype='U' and name='表名') order by sc.colid; --查询字段类型
  • 查询sqlserver表字段的中文注释
select value from sys.extended_properties where major_id = object_id ('表名' );
  • 查询sqlserver表的中文注释(中文表名)
SELECT tbs.name 表名,ds.value 描述 FROM sys.extended_properties ds LEFT JOIN sysobjects tbs ON ds.major_id=tbs.id  WHERE  ds.minor_id=0  and tbs.name='表名' ORDER BY tbs.name; --引号里输入表名
  • 查询sqlserver表前几行
select top 5 * from table;

=========================================================
在网上查到的一个别人写的汇总,测试后可以直接使用,非常方便!!!来自(https://www.cnblogs.com/qy1234/p/9044275.html)

------sqlserver 查询某个表的列名称、说明、备注、类型等
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 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 '√' 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
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='表名'    
order by
    a.id,a.colorder; --引号里面输入表名

sqlserver数据库及表信息查询_第1张图片

你可能感兴趣的:(数据库)