all groups > sql server programming > january 2005 >
You're in the

sql server programming

group:

how to add new column like existing one?


Re: how to add new column like existing one? Louis Davidson
1/29/2005 5:22:45 PM
sql server programming: Add the column, run an update statement:

create table tablename
(
x int
)
insert into tablename values (1)
insert into tablename values (2)
insert into tablename values (3)
go
alter table tablename add y int null
go
update tablename
set y = x+3
go
alter table tablename alter column y in not null
go

Will you want all new values to be x + 3, or just initially? If the ONLY
values ever to be stored would be then you can use a computed column and
index it (this will persist the values so it doesn't have to be calculated
except the first time:

alter table tablename add y1 as (x+3)

create index tablnamePersistY1 on tablename(y1)

--
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP

Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)

[quoted text, click to view]

how to add new column like existing one? AFN
1/29/2005 9:44:12 PM
What's the best way to add a new column Y to an existing table that already
has existing column X, and I want Y to be x+3? I need to actually save the
value in the database, so yes, I do want a new column.

Re: how to add new column like existing one? Hugo Kornelis
1/30/2005 12:13:57 AM
[quoted text, click to view]

Hi AFN,

ALTER TABLE MyTable
ADD Y AS x+3

Best, Hugo
--

AddThis Social Bookmark Button