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.
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.
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
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
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