Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a column of zeros to table sql server [duplicate]

Tags:

sql-server

Possible Duplicate:
Add column, with default value, to existing table in SQL Server

I have a table in sql server, but want to add a extra column full of zeros

What would be the best approach to do this

att1 att2 --------- 1.0   5.8 2.7   3.8 5.1   6.8 

becomes

att1 att2  extra ---------------- 1.0   5.8   0.0 2.7   3.8   0.0 5.1   6.8   0.0 
like image 494
edgarmtze Avatar asked Apr 26 '12 23:04

edgarmtze


2 Answers

If I recall correctly, it should be something like:

ALTER TABLE table_name  ADD extra REAL DEFAULT 0 

See: http://msdn.microsoft.com/en-us/library/ms190273.aspx

See: Add a column with a default value to an existing table in SQL Server

like image 65
hookenz Avatar answered Oct 17 '22 15:10

hookenz


I understand this column will always have value of 0.0. Then it does not have to be a real column

CREATE TABLE extraColumn (      att1   float  NULL     ,att2   float  NULL     ,extra AS 0.0  ) 

But if you need that, you can persist it

CREATE TABLE extraColumn (      att1   float  NULL     ,att2   float  NULL     ,extra AS 0.0  PERSISTED  ) 
like image 35
satcat66 Avatar answered Oct 17 '22 14:10

satcat66