views:

72

answers:

3

Hi all, At my work we are currently having some serious pains pushing our database changes across environments. The issue starts to show up when we create a new non-nullable column on an existing table. The script that SQL Compare generates creates the column as non-nullable, so it will always fail. I was hoping that there was some alternative to having to manually edit the script. Is there any way to get around this? If not, how do you guys handle it?

A: 

How do you plan on filling the NOT NULL column? I don't see how SQL Compare could really come up with a solution since it has no way of knowing how you would fill it.

You could create the column with a DEFAULT, then simply add an update statement at the end of the generated scripts to properly update the column if you have some source for the values.

Tom H.
I was hoping there was a way to set it up so that it created all of the columns as nullable, then left a space for me to update the table, and then alter the column to be non-nullable.
Correl
A: 

Create a table:

create table #bingo ( id int )

Add a value:

insert into #bingo values (1)

Add a new column:

alter table #bingo add userid int

Populate the new column:

update #bingo set userid = 1 where id = 1

Change the new column to not nullable:

alter table #bingo alter column userid int not null

You would have to manually edit the RedGate Sql Compare to make it work like this.

Andomar
A: 

add a default onto the new not null column

when creating a new not null column, what value do all the existing rows get? If you don't know, make the column nullable.

KM
It depends on the change, but generally it is pulled from within the database itself.
Correl
@Correl, how can you ever expect a new not null column on an existing table (with rows) to ever work, without a default? You may want to remove the default after the column is created, to force your application to insert a good value.
KM
I was hoping that RedGate would give me a place to insert the values in the script. Like create the column as nullable, leave a blank section for me to add my custom logic and then alter the column as non-nullable. I can do this by hand, but I was hoping that RedGate could make it easier.
Correl