查询字段的默认值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查询字段的默认值相关的知识,希望对你有一定的参考价值。

--使用在SQL2008,***查询字段说明和默认值
select   object_name(c.id) as 表名   ,c.name as 字段名   ,t.name as 数据类型   ,c.prec as 长度   ,
p.value as 字段说明   ,m.text as 默认值  
from syscolumns c   inner join   systypes t   on c.xusertype=t.xusertype   
left join  sys.extended_properties p   on c.id=p.major_id and c.colid = p.minor_id   
left join   syscomments m   on c.cdefault=m.id
-------------------------------------------------------------------------------------------
select * from sys.extended_properties  --SQL2008 扩展属性表(字段注释,)


----------------------------------------------------------------------------------------------------

/*在sql2000中使用*/

--获取所有默认值:

select   object_name(c.id) as 表名   ,c.name as 字段名   ,t.name as 数据类型   ,c.prec as 长度   ,
p.value as 字段说明   ,m.text as 默认值  from syscolumns c   
inner join   systypes t   on c.xusertype=t.xusertype   
left join    sysproperties p   on c.id=p.id and c.colid = p.smallid   
left join   syscomments m   on c.cdefault=m.id

--获取单个字段默认值:

select   object_name(c.id) as 表名   ,c.name as 字段名   ,t.name as 数据类型   ,c.prec as 长度   ,
p.value as 字段说明   ,m.text as 默认值  from syscolumns c   
inner join   systypes t   on c.xusertype=t.xusertype   
left join    sysproperties p   on c.id=p.id and c.colid = p.smallid   
left join   syscomments m   on c.cdefault=m.id  
where objectproperty(c.id,‘IsUserTable‘)=1   and object_name(c.id) = ‘表名‘ and c.name = ‘字段名称‘  




---------------------------------------------------------------------------------------------------
--查询指定表的指定字段的默认值
select   object_name(c.id) as 表名   ,c.name as 字段名   ,t.name as 数据类型   ,c.prec as 长度   ,
p.value as 字段说明   ,m.text as 默认值  from syscolumns c   
inner join   systypes t   on c.xusertype=t.xusertype   
left join  sys.extended_properties p   on c.id=p.major_id and c.colid = p.minor_id    
left join   syscomments m   on c.cdefault=m.id  
where objectproperty(c.id,‘IsUserTable‘)=1   and object_name(c.id) = ‘表名‘ and c.name = ‘字段名‘  










 
 
 

  

以上是关于查询字段的默认值的主要内容,如果未能解决你的问题,请参考以下文章

查询字段的默认值

如何从数据库查询各表中所有存在默认值的字段

Mongoid 按值或默认值查询

mysql查询语句如何将默认值设置为当前时间

Elasticsearch--搜索

查询表字段类型是否主键长度小数位是否允许空默认值等详细查询