rmatthew Posted January 26, 2006 Posted January 26, 2006 I am trying to alter a column within a table and set the default value to 0: I am using SQL Server 2000 here is what I am using (it gives 'incorrect syntax near DEFAULT') alter table chronology alter column oper_id numeric(10,0) set default 0 not null Quote
*Experts* Nerseus Posted January 26, 2006 *Experts* Posted January 26, 2006 Your syntax is close, but a bit off. At least, it's off if you're using SQL Server: alter table chronology alter column oper_id numeric(10,0) not null DEFAULT 0 At least, that's the syntax when using "add" column, not sure if you can set a default when altering a column. I would think so, but not positive. If not, you can always do a mass update followed by an alter that sets the column to NOT NULL. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
rmatthew Posted January 26, 2006 Author Posted January 26, 2006 This works (as you stated) for an add, but no such luck on an alter column. Unsure what you mean by mass update and then alter (without loosing data). Quote
*Experts* Nerseus Posted January 26, 2006 *Experts* Posted January 26, 2006 Yep, I read the help more and see there's no option to set a default for an existing column that would update the data. Assuming you've already got the column in the table but want to change to not null, do the mass update before altering the table/column: UPDATE chronology SET oper_id = 0 WHERE oper_id IS NULL GO -- The GO is optional alter table chronology alter column oper_id numeric(10,0) not null -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.