/*-- 将所有的表中,数值型的默认值设置为0--*//*--调用示例:
exec p_set
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_set]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_set]
GO--用下面的存储过程
create procedure p_set
as
declare tb cursor for
SELECT 表名=d.name,字段名=a.name
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
where
a.status<>0x80 --标识列也不能设置默认值
and (b.name like '%int' or b.name like '%money'
or b.name in('decimal','numeric','float','real'))
and
not exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
)))
order by d.name,a.namedeclare @tbname sysname,@fdname sysname
open tb
fetch next from tb into @tbname,@fdname
while @@fetch_status = 0
begin
exec(' alter table ' + @tbname + ' add CONSTRAINT df_'+@fdname+ ' default 0 for ' + @fdname )
fetch next from tb into @tbname,@fdname
end
close tb
deallocate tb
go
exec p_set
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_set]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_set]
GO--用下面的存储过程
create procedure p_set
as
declare tb cursor for
SELECT 表名=d.name,字段名=a.name
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
where
a.status<>0x80 --标识列也不能设置默认值
and (b.name like '%int' or b.name like '%money'
or b.name in('decimal','numeric','float','real'))
and
not exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
)))
order by d.name,a.namedeclare @tbname sysname,@fdname sysname
open tb
fetch next from tb into @tbname,@fdname
while @@fetch_status = 0
begin
exec(' alter table ' + @tbname + ' add CONSTRAINT df_'+@fdname+ ' default 0 for ' + @fdname )
fetch next from tb into @tbname,@fdname
end
close tb
deallocate tb
go
exec p_setbit
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_setbit]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_setbit]
GO--用下面的存储过程
create procedure p_setbit
as
declare tb cursor for
SELECT 表名=d.name,字段名=a.name
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
where b.name='bit'
order by d.name,a.namedeclare @tbname sysname,@fdname sysname
open tb
fetch next from tb into @tbname,@fdname
while @@fetch_status = 0
begin
exec(' alter table ' + @tbname + ' alter column ['+@fdname+'] bit null')
fetch next from tb into @tbname,@fdname
end
close tb
deallocate tb
go