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
 General SQL Server Forums
 New to SQL Server Programming
 Script to change column from nullable?

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2009-03-17 : 05:27:48
Is it possible to write a script to change a table's column from nullable to not null as we have a load of the same databases on several servers and I don't really want to manually do this.

If it's possible can you tell me what the script is.

Many thanks

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2009-03-17 : 05:30:37
Yes you can provided either the table is empty or the column contains non-null values.

Following is the command for this:

alter table <table-name>
alter column foo int not null


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sagitariusmzi
Posting Yak Master

113 Posts

Posted - 2009-03-17 : 05:45:34
alter table tablename alter column columnname datatype not null
go
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2009-03-17 : 05:47:17
quote:
Originally posted by harsh_athalye

Yes you can provided either the table is empty or the column contains non-null values.

Following is the command for this:

alter table <table-name>
alter column foo int not null




Thats great, thanks it saved me a lot of work
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-17 : 10:36:29
This will work if table does not have any data. if it has existing data with null values in column you need to specify a default value also

alter table <table-name>
alter column foo int not null default -1
Go to Top of Page
   

- Advertisement -