Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

ALTER TABLE MyTable
ALTER COLUMN OldCol1 bit NOT NULL

ALTER MyTable
ADD CONSTRAINT OldCol1ShouldBeFalse DEFAULT 0 FOR OldCol1

I set default to 0 if no value is supplied to this column ...
But now when i try to insert a row into this table without supplying any value to this column ..

I was expecting that default should set to 0 automatically .. but it throwing an exception that unbale to insert null to this column .. why so ..?? it should insert my default value ..
Posted
Updated 31-Oct-13 20:24pm
v2
Comments
Ashwani Gusain 1-Nov-13 2:33am    
post your sql command also..
[no name] 1-Nov-13 2:41am    
ALTER TABLE MyTable
ALTER COLUMN OldCol1 bit NOT NULL

ALTER MyTable
ADD CONSTRAINT OldCol1ShouldBeFalse DEFAULT 0 FOR OldCol1
[no name] 1-Nov-13 2:58am    
I am just adding into table using linq to sql ..
Madhu Nair 1-Nov-13 2:57am    
Post your Insert Query
[no name] 1-Nov-13 3:08am    
table has got huge number of columns ,,, this new column ISDeleted i have added now .. but the rest the coding is working fine ... there are no changes on previous code.

Hi torakami.

This is how the SQL statement should be looking like for setting the Default value of a column to Zero in SQL Server.

SQL
ALTER TABLE [dbo].[MyTable]
ADD  CONSTRAINT [OldCol1ShouldBeFalse]  DEFAULT ((0)) FOR [OldCol1]


Try this one and let us know the result.

Thanks,
RelicV
 
Share this answer
 
SQL
ALTER TABLE TableName
ADD DEFAULT (DefaultValue) FOR ColumnName
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900