如何从数据库查询各表中所有存在默认值的字段
如题,执行添加字段注释的时候不小心忽略了默认值约束,覆盖了很多字段,现已回滚,想执行一条sql语句查询出数据库中200多张表所有存在默认值的字段,手动找出效率太低。...
如题,执行添加字段注释的时候不小心忽略了默认值约束,覆盖了很多字段,现已回滚,想执行一条sql语句查询出数据库中200多张表所有存在默认值的字段,手动找出效率太低。
展开
2个回答
展开全部
-- sql server我就知道,其他没用过。
-- tableName表名,sqlName列名,defaultVal就是默认值,字符串表示
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询