求一SQl:获取表或视图的字段名、字段类型、是否为主键等信息

时间:2022-07-28 13:49:25
在网上找了一个,查询结果看不懂啥意思。
SELECT *
FROM syscolumns
WHERE (id = OBJECT_ID('spt_monitor'))
          SELECT *
        FROM systypes
        WHERE (xtype = xusertype) AND (xtype IN
                  (SELECT xtype
                 FROM syscolumns
                 WHERE id =
                           (SELECT id
                          FROM sysobjects
                          WHERE name = 'spt_monitor' AND xtype = 'U')))

6 个解决方案

#1



--獲取表spt_monitor的所有字段
SELECT *
FROM syscolumns
WHERE (id = OBJECT_ID('spt_monitor'))

#2


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 
    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='要查询的表'    --如果只查询指定表,加上此条件
order by 
    a.id,a.colorder

#3


2楼,太复杂了吧?看不懂啊

#4


up

#5


select   object_name(id)   as   表名   
,c.name   as   字段名   
,t.name   数据类型   
,c.prec   as   长度   
from   syscolumns   c   
inner   join   
systypes   t   
on   c.xusertype=t.xusertype   
where   objectproperty(id,'IsUserTable')=1 and id=object_id('tb')

/*
表名                                                                                                                               字段名                                                                                                                              数据类型                                                                                                                             长度     
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------ 
tb                                                                                                                               id                                                                                                                               int                                                                                                                              10
tb                                                                                                                               sid                                                                                                                              varchar                                                                                                                          10
tb                                                                                                                               course                                                                                                                           varchar                                                                                                                          10
tb                                                                                                                               result                                                                                                                           float                                                                                                                            53


*/

#6


用sp_help 吧

#1



--獲取表spt_monitor的所有字段
SELECT *
FROM syscolumns
WHERE (id = OBJECT_ID('spt_monitor'))

#2


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 
    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='要查询的表'    --如果只查询指定表,加上此条件
order by 
    a.id,a.colorder

#3


2楼,太复杂了吧?看不懂啊

#4


up

#5


select   object_name(id)   as   表名   
,c.name   as   字段名   
,t.name   数据类型   
,c.prec   as   长度   
from   syscolumns   c   
inner   join   
systypes   t   
on   c.xusertype=t.xusertype   
where   objectproperty(id,'IsUserTable')=1 and id=object_id('tb')

/*
表名                                                                                                                               字段名                                                                                                                              数据类型                                                                                                                             长度     
-------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- ------ 
tb                                                                                                                               id                                                                                                                               int                                                                                                                              10
tb                                                                                                                               sid                                                                                                                              varchar                                                                                                                          10
tb                                                                                                                               course                                                                                                                           varchar                                                                                                                          10
tb                                                                                                                               result                                                                                                                           float                                                                                                                            53


*/

#6


用sp_help 吧