|
|
Question : How can I Define a Default integer in SQL Server 2005 & make it work?
|
|
I want to define a default value of "6". When I define the Default Value or Bindings in the Column Properties for my column, it gets converted to ((6)) but then does nothing. When I insert a new row the default remains NULL. I would like it to be a 6. Any ideas?
|
Answer : How can I Define a Default integer in SQL Server 2005 & make it work?
|
|
The alter column doesn't put the DEFAULT in either - meant to, but doesn't.
If it is being populated with NULL then having DEFAULT as part of the column should work - it wont work if an empty string ie '' (single-quote single-quote)
easiest way to get default on the column is to create a new column, populate it, drop the old column, recreate with DEFAULT, repopulate and drop the new. Can all be done in T-SQL. The column does not have to be "not null".
Alternatively, create a trigger which checks the column e.g. update mytable set col1 = 6 where isnull(col1,'') = '' from mytable, inserted where mytable.id = inserted.id
Alternatively, trap the problem at the source and if after making the selection it is NULL or zero length, then move the '6'.
Do you want to see the code to "migrate" the column to have the default ? Mind you, the trigger is probably the most failsafe because it will also capture null and zero length strings...
|
|
|
|
|