Mathematics
Mathematics

Reputation: 7628

How to add ten new columns to SQL table with 1000+ records in it without losing any single record

I am looking for query that I can use to add ten new boolean columns to existing table without effecting anything else in database. Also by default these boolean columns should be set to true as default.

Looking for best practices.

Edit

"Also by default these boolean columns should be set to true as default."

Sorry I meant to say that for records already exists new column should have value set as true, there shouldn't be any default value as I will set it myself.

Upvotes: 0

Views: 8086

Answers (1)

stylefish
stylefish

Reputation: 571

just add the columns via an ALTER statement:

ALTER TABLE MyTable ADD MyColumn bit NULL

or not nullable with a default value:

ALTER TABLE MyTable ADD MyColumn bit NOT NULL default 1

thats it for MSSQL.

EDIT:

if you want to add multiple columns at a time you can use:

ALTER TABLE MyTable ADD MyCol1 bit NOT NULL DEFAULT 1, MyCol2 bit NULL
GO

the "go" completes the batch.

Upvotes: 6

Related Questions