Search code examples
c#asp.netsql-servervisual-studio-2005

Update older records when a new column is added


In my system, the client will fill in their information to register an account. For new registrations, the record will contain a new check-box option.

My Question is, how do I update the old records so that they contain a checked value for the check-box option that has been added?

My supervisor suggest I patch the data but I'm not sure where to start with this.


Solution

  • I assume you want to add a new bit column to an existing table and want all existing records have the value 1 in it. To do this you first create the new column and than right after set all existing records to 1

    alter table MyTable
    add CheckedField bit
    
    update MyTable set CheckedField = 1
    

    If the new column is mandatory than you can do it like this :

    alter table MyTable
    add CheckedField bit not null default (1)