views:

140

answers:

4

Hi

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.

+2  A: 
ALTER TABLE tablename ALTER COLUMN CompanyID SET DEFAULT 1;
kibitzer
I have tried this, it is not working.The error is 'Incorrect syntax near the keyword 'SET'.'
I think it can't be done in this way in sql server 2000
Jonathan
+3  A: 

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
Tim C
+5  A: 

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
Jonathan
Thank dude, i is working fine. Thanks a lot.
A: 

I have the same problem:

ALTER TABLE MyTable ALTER COLUMN MyColumn SET DEFAULT MyDefault

does not work for me on SQL Server 2008. I got the same error:

'Incorrect syntax near the keyword 'SET'.'

Should I drop all constraints on this table before adding again?

Vedran

Vedran