Replace nullable column with non nullable in MS SQL Database
Needed to replace a nullable
column in a database with a non nullable
column that. In this case a boolean
field needed to transition so came up with this to alter the structure it. The following statement does not transition the contents of the original column to the new column. For me this is acceptable as the application code was written to be idempotent and would reload the appropriate data as soon as it is called again (this was achieved with a explicit matching set of fields and a merge
statement)
USE [target_db];
ALTER TABLE [dbo].[target_table] ADD [column_to_add] [bit] NOT NULL CONSTRAINT [DF_target_table_column_to_add] DEFAULT 0;
ALTER TABLE [dbo].[target_table] DROP COLUMN [column_to_replace]