SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to make a particular column null in database
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 03/19/2013 :  04:11:30  Show Profile  Reply with Quote
hello all,

i have two column created and modified columns in all tables in database.Almost these columns will come in all tables in entire database.So is there any way to make null these columns in one go like 300 tables. Suggest me .....

P.V.P.MOhan

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 03/19/2013 :  04:32:53  Show Profile  Reply with Quote
If all tables have created and modified columns, then use the follwoing sql code:

exec sp_MSforeachtable 'ALTER TABLE ? ALTER Column created DATETIME NULL'
GO
exec sp_MSforeachtable 'ALTER TABLE ? ALTER COLUMN modified DATETIME NULL'

NOTE: sp_MSforeachtable is undocumented procedure.So do not use in production server

--
Chandu
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 03/19/2013 :  04:51:41  Show Profile  Reply with Quote
while executing the above query if the table have created column it is making null but if the table doesn't have created column it is showing error like

ALTER TABLE ALTER COLUMN failed because column 'Modified' does not exist in table

how to overcome this one

P.V.P.MOhan
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 03/19/2013 :  05:24:41  Show Profile  Reply with Quote
Try this once........

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM sys.columns WHERE table_name = ? and column_name = ''created'' )
	ALTER TABLE ? ALTER Column created DATETIME NULL'

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM sys.columns WHERE table_name = ? and column_name = ''modified'' )
	ALTER TABLE ? ALTER Column created DATETIME NULL'


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/19/2013 :  10:17:25  Show Profile  Reply with Quote
it should be

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE ''['' + table_Schema + ''].['' + table_name + '']'' = ''?'' and column_name = ''created'' )
	ALTER TABLE ? ALTER Column created DATETIME NULL'

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE ''['' + table_Schema + ''].['' + table_name + '']'' = ''?'' and column_name = ''modified'' )
	ALTER TABLE ? ALTER Column modified DATETIME NULL'



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/19/2013 :  10:19:34  Show Profile  Reply with Quote
quote:
Originally posted by bandi

Try this once........

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM sys.columns WHERE table_name = ? and column_name = ''created'' )
	ALTER TABLE ? ALTER Column created DATETIME NULL'

exec sp_MSforeachtable 
'IF EXISTS ( SELECT 1 FROM sys.columns WHERE table_name = ? and column_name = ''modified'' )
	ALTER TABLE ? ALTER Column created DATETIME NULL'


--
Chandu


wont work like above

1. ? returns fully qualified tablename ie [schema].[tablename]
2. sys.columns doesnt have table_name column so should be using INFORMATION_SCHEMA.COLUMNS table instead
3. for using table name as string use '' around ?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 03/20/2013 :  00:40:05  Show Profile  Reply with Quote
Yes... that is my mistake...
I haven't checked above query with sys.columns....

--
Chandu
Go to Top of Page

mohan123
Constraint Violating Yak Guru

India
252 Posts

Posted - 03/20/2013 :  00:43:54  Show Profile  Reply with Quote
yeah thanks...my work got over in first question itself

ALTER table name check check constraint ALL this query done trick for making all noCHECK constraint to Check constraint ....thanks chandu and visakh

P.V.P.MOhan
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 03/20/2013 :  00:51:54  Show Profile  Reply with Quote
quote:
Originally posted by mohan123

yeah thanks...my work got over in first question itself

ALTER table name check check constraint ALL this query done trick for making all noCHECK constraint to Check constraint ....thanks chandu and visakh
P.V.P.MOhan

Welcome...

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000