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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Computed from another table

Author  Topic 

lamchau12
Starting Member

2 Posts

Posted - 2010-03-18 : 08:45:22
Hi all,

I have three tables : A(id, columnA); B(id, columnB) and C(id, columnC)
i want to use computed for columnC with following formula :

columnC = A.columnA + B.columnB


How could i do that?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-18 : 12:47:13
[code]
UPDATE c
SET c.columnC=COALESCE(a.columnA,'') + COALESCE(b.columnB,'')
FROM C c
JOIN B b
OON b.id=c.id
JOIN A a
ON a.ID=b.ID
[/code]
if columns are numeric replace '' with 0

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lamchau12
Starting Member

2 Posts

Posted - 2010-03-19 : 04:18:16
Thank you for your support. But i meant using computed attribute of sql server 2008

Ex :
CREATE TABLE [dbo].[C](
[id] [nvarchar](50) NULL,
[column3] AS ([ColumnA]+[ColumnB])
)


How could i use computed attribute to calculate value of Column3 automatically? I'm already using trigger in this case. But my data is too large. Therefore i need to use computed attribute in this case.
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-19 : 04:38:55
quote:
Originally posted by lamchau12

Thank you for your support. But i meant using computed attribute of sql server 2008

Ex :
CREATE TABLE [dbo].[C](
[id] [nvarchar](50) NULL,
[column3] AS ([ColumnA]+[ColumnB])
)


How could i use computed attribute to calculate value of Column3 automatically? I'm already using trigger in this case. But my data is too large. Therefore i need to use computed attribute in this case.




Are you trying for this?
http://www.mssqltips.com/tip.asp?tip=1682
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-19 : 04:54:59
Another option is to have a view

Create view your_view
as
select t1.cola,t2.colb,t1.cola+t2.colb as colc
from A as t1 inner join B as t2 on t1.id=t2.id

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -