user92027
user92027

Reputation: 391

Alter column's default value

I have a table which has a column 'CompanyID int not null' and its default value is set to 10. Now I want to write a query which will alter this default value to 1. How can can I do it?

Any help will be appreciated. I am using SQL server 2000.

Upvotes: 7

Views: 16928

Answers (4)

user3484841
user3484841

Reputation: 1

ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT Pending;

I have tried this in mysql and it was giving error near Pending

Then I tried

 ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT 'Pending';

which worked fine.

Upvotes: -1

Tim C
Tim C

Reputation: 70648

First, find out the name of the 'constraint' on the field which is used to set the default. You can do this by running this query:

EXEC sp_helpconstraint 'MyTable'

Then, you can just drop and re-add the constraint.

ALTER TABLE dbo.MyTable
DROP CONSTRAINT def_MyTable_CompanyID
GO

ALTER TABLE dbo.MyTable
ADD CONSTRAINT def_MyTable_CompanyID DEFAULT (1) FOR CompanyID
GO

Upvotes: 9

Jonathan
Jonathan

Reputation: 12015

I think the best you can do is drop the constraint and create it again:

alter table dbo.yourTable
drop constraint default_value_name_constraint
go
alter table dbo.yourTable
add constraint default_value_name_constraint default YourValue for ColumnName
go

Upvotes: 17

kibitzer
kibitzer

Reputation: 4589

ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT 1; 

Upvotes: 1

Related Questions