Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Add Clolumn in a table

Author  Topic 

dr223
Constraint Violating Yak Guru

444 Posts

Posted - 2011-05-20 : 10:42:52
Hi,

I have a table called dbo.tblfruits as shown below

A B
1 Orange
2 Banana
3 Apple
4 Peas

I want to add another column to dbo.tblfruits Column C, this column should come from another table dbo.tblprice, whereby COLUMN A on dbo.tblfruits = COLUMN A on dbo.tblprice..

dbo.tblprice is as shown below;

A B
1 2.00
2 3.00
3 4.00
4 4.00

Therefore, my final results will be;

A B C
1 Orange 2.00
2 Banana 3.00
3 Apple 4.00
4 Peas 4.00


Many thanks


nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-05-20 : 11:40:14
alter table tblfruits add C decimal(10,2) not null default 0
update tblfruits
set C = p.B
from tblfruits f
join tblprice p
on f.A = p.A

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2011-05-20 : 14:21:06
If it is already in another table, and there is a foreign key to it, then LEAVE IT ALONE. 1st Normal Form = good.



http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -