Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Defaults

Author  Topic 

DavidD
Yak Posting Veteran

73 Posts

Posted - 2003-07-28 : 19:49:51
Hi all,

I am trying to alter a column to add a default definition to it using the following syntax

alter table MyTable
alter column MyColumn int default 0

I keep getting the error 'incorrect syntax near the keyword default'
I can perform this operation using a default object, but according to BOL this is a legacy technique and the technique I am using should work fine - it does in the create table statement.
Does anyone know what the story is?

Thanks in advance
David

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2003-07-28 : 19:53:14
ALTER TABLE Table1 ADD CONSTRAINT
DF_Table1_MyColumn DEFAULT 0 FOR MyColumn

Tara
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-07-28 : 19:53:15
Try:

ALTER TABLE MyTable ALTER COLUMN MyColumn int DEFAULT(0)
Go to Top of Page

DavidD
Yak Posting Veteran

73 Posts

Posted - 2003-07-28 : 20:55:55
Hi Rob + Tara,

Thanks for your replies, but
Tara - your technique uses the default object I mentioned I am trying to avoid using, according to BOL it is a legacy technique, but it seems to be the only one I can get to work.

Rob - I get the same error as previously with your syntax. Does that command work for you? I am on SQL 2000 with SP3.

Regards
David
Go to Top of Page

shsmonteiro
Constraint Violating Yak Guru

290 Posts

Posted - 2003-07-29 : 00:13:01
Hi David,

Tara's syntax is the only one supported by SQL Server. But you don't need to name the default or even specify the constraint word. That is you can write

ALTER TABLE Table1 ADD CONSTRAINT
DF_Table1_MyColumn DEFAULT 0 FOR MyColumn

or
[code]ALTER TABLE Table1 ADD DEFAULT 0 FOR MyColumn[/0]

Sérgio
Go to Top of Page

DavidD
Yak Posting Veteran

73 Posts

Posted - 2003-07-29 : 00:50:31
Thanks Sergio

Go to Top of Page
   

- Advertisement -