Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Set Nulls to Default

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-12-17 : 10:45:21
Hi,
I am looking for a stored procedure like sp_msforeachtable that by using it set all null values to default,
I mean all columns in all tables in a database.

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-12-17 : 11:46:57
Execute this Query and run the result set

select 'Alter table '+o.name +' add constraint DF_cont_'+o.name+'_'+c.name
+' Default null for '+c.name
from sys.objects o inner join sys.columns c on c.object_id=o.object_id
where o.type='u'

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-12-17 : 12:12:48
Thanks, your idea is great but I was looking for this:

select 'update '+o.name +' set ' +c.name +'= default '
+' where '+c.name+ ' is null'
from sys.objects o inner join sys.columns c on c.object_id=o.object_id
where o.type='u'

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-18 : 01:23:46
quote:
Originally posted by ms65g

Thanks, your idea is great but I was looking for this:

select 'update '+o.name +' set ' +c.name +'= default '
+' where '+c.name+ ' is null'
from sys.objects o inner join sys.columns c on c.object_id=o.object_id
where o.type='u'




Then you need this


select 'update '+table_name +' set ' +column_name +'= '+column_default
+' where '+column_name+ ' is null '
from information_schema.columns
where column_default is not null
and objectproperty(object_id(table_name),'istable')=1


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-12-18 : 02:30:13
quote:
Originally posted by ms65g

Thanks, your idea is great but I was looking for this:

select 'update '+o.name +' set ' +c.name +'= default '
+' where '+c.name+ ' is null'
from sys.objects o inner join sys.columns c on c.object_id=o.object_id
where o.type='u'





Welcome

Have you execute madhi's Query??

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page
   

- Advertisement -