Setting a Default for a New Column
juliajuliabell
Posts: 10
Is it possible to set a default value for a newly created NOT NULL column, without actually setting a default value in the database?
I guess in order to do this the script would have to create the column as NULL, update it with the default value and then alter it to NOT NULL. Or something along those lines.
I guess in order to do this the script would have to create the column as NULL, update it with the default value and then alter it to NOT NULL. Or something along those lines.
Comments
This isn't possible right now, although it's something we're hoping to add soon. Can I ask whether you'd prefer to specify your own default, or if your source database has actual data values from which the values going into the new column can be populated?
David Atkinson
Product Manager
Red Gate Software
Product Manager
Redgate Software
Julia.
David
Product Manager
Redgate Software
Thanks for the reply. I could do that, but I was looking for a solution that was more automated. The more manual steps there are, the more chance there is of introducing an error into the process.
Specifically, there's two ways I would like to see this implemented:
- Add a default to the temp tables created when copying the data.
- Add a default value to the SELECT used to copy data to the temp table (e.g. INSERT INTO [tmp_rg_XXX] ([col1],[col2],[col3]) SELECT [col1], [col2], 0 FROM [OldTable] ...)
That is, I don't think a default should be added to the actual tables. If you want that, then I agree that you should just add the default to the new or old table before doing the compare.
I'm making changes to an existing software package. When I start a fix or feature upgrade process, I SQL Data Compare the data into my Dev database, make my changes, including new fields, with defaults, and some NOT NULL.
Because I then Sync back to the existing Prod db when it's ready, I start with SQL Compare, then do the Data Compare second. SQL Compare tries to create the new field as NOT NULL which it can't do with some data.
I would prefer that the default is pulled from the schema, in case it's a formula, but for me it doesn't really matter since I'm most likely going to do a Data Compare right afterwords.
http://www.red-gate.com/MessageBoard/vi ... 1312#51312
Let us know if this satisfies your needs.
David Atkinson
Red Gate Software
Product Manager
Redgate Software
Would you consider adding this to SQL Compare.
We are currently researching ways to extend migrations to SSDT projects, allowing users to use this functionality without using Source Control, and I would be really interested learning more about your requirements for a solution. Do you think you would be available for a brief research call (ca. 30 minutes)? If you are, would you mind dropping me a line at daniel.rothig [at] red-gate.com for more details?
Many thanks
Daniel Rothig
Product Manager
Red Gate Software