Adding Multiple Columns With Default Value

Almost a decade before, I came across a requirement where I needed to add multiple columns in a table with default value. It took a while on that time to figure it out.

I found it in my old SQL Projects repository and thought to share it with you.

— Creating a table and adding a row
CREATE TABLE TestTable1(Id INT, SomeCol VARCHAR(10))
INSERT INTO TestTable1 SELECT 10, ‘abcdefgh

— Adding Multiple Columns With Default Values
ALTER TABLE TestTable1
                        ADD    Col1 VARCHAR(100) NOT NULL DEFAULT(‘SomeInformation‘),
                                    Col2 INT NOT NULL DEFAULT(999)
GO

— Retrieve Data from Table
SELECT * FROM TestTable1
GO

MultipleColumnsWithDefaultValue

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s