Author |
Topic |
hai
Yak Posting Veteran
84 Posts |
Posted - 2008-10-01 : 17:36:37
|
Hi all,I need to update my table columns to a default value. Beside get into the Management Studio to updated every column...does anyone have a script to updated it?thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 17:45:41
|
what do you mean?do you have a table likesometablecol1 intcol2 datetimecol3 varchar(255)and want to update every col3 to 'default_whatever' only one time?Or do you want to something other?WebfredPlanning replaces chance by mistake |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 17:46:51
|
sorry tara - you're too fast 4 mePlanning replaces chance by mistake |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 17:48:04
|
maybe it's because youre mother and i am already grandpa ;o)Planning replaces chance by mistake |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
hai
Yak Posting Veteran
84 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 03:57:12
|
quote: Originally posted by hai Hi Tara,yes...adding a default constraint to the columns if is null then set it to 0 for int and blank for varchar.thanksquote: Originally posted by tkizer Do you mean adding a default constraint to the columns or updating the data directly?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
create default constraint using ALTER TABLE ALTER COLUMNALTER TABLE yourtablename ALTER COLUMN yourintcolumnname int NOT NULL DEFAULT 0ALTER TABLE yourtablename ALTER COLUMN yourvarcharcolumnname varchar(30) NOT NULL DEFAULT 'Default Value'...then during inserts either dont include then in insert list at all or useINSERT INTO yourtable (othercolumns, intcol,varcharcol)VALUES( values for others..,DEFAULT,DEFAULT)and for updatesUPDATE yourtableSET intcol=DEFAULT,varcharcol=DEFAULT...and if you dont want to explicitly include them in update then you need a update trigger to update default values automatically as default constraints will automatically applied only on inserts and not in updates. |
 |
|
|