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 2005 Forums
 Transact-SQL (2005)
 Set bit field to default false

Author  Topic 

MyronCope
Starting Member

46 Posts

Posted - 2007-04-16 : 15:11:09
Hi,

I have a bit field that I will just call FieldA for now, anyways, I need to write a script that will modify the bitfield so that when a new record is created in that table, lets call it tblSomeTable, that it will set FieldA to False (O).

Thanks

pootle_flump

1064 Posts

Posted - 2007-04-16 : 15:18:35
[code]ALTER TABLE tblSomeTable
ADD FieldA BIT --NOT NULL
CONSTRAINT df_tblSomeTable_FieldA DEFAULT (0)[/code]Comment out the NOT NULL if you don't want nulls.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-16 : 15:23:29
I think he just needs the constraint since the column already exists, or at least that's how I read it.

ALTER TABLE tblSomeTable ADD CONSTRAINT df_tblSomeTable_FieldA DEFAULT (0)

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-04-16 : 15:25:29
Ah! Whoops a daisy.

Does that work though? Column name not specified....

ALTER TABLE tblSomeTable
ADD CONSTRAINT df_tblSomeTable_FieldA DEFAULT 0 FOR FieldA
Go to Top of Page

MyronCope
Starting Member

46 Posts

Posted - 2007-04-16 : 15:30:18
Yes, tk, the field already exists, i just want to set the default value to 0, thanks much to both of you
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-16 : 15:41:41
You may also think about adding the WITH VALUES
if you intend to default all existing null values to 0.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -