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)
 How to update table

Author  Topic 

kabon
Starting Member

48 Posts

Posted - 2013-03-05 : 23:42:41
I have 2 table that i must update table 1 and take the data from table 2
that table 2 is history file
for example :
table 1
Name | Class | Number |
nick | | |
james | | |
Moo | | |

table 2

Name | Class | Number |
nick | 2E | 54 |
james | 3C | 66 |
Moo | 4D | 98 |

do you know how to update table 1 if data from table 2 to is so much and just using indicator Name? please help me for the script

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-05 : 23:49:12
Can there be more than one row for a given name? Assuming that that is not the case, i.e., there is only one row for any one name:
UPDATE t1 SET
Class = t2.Class,
Number = t2.Number
FROM
Table1 t1
INNER JOIN Table2 t2 ON
t1.name = t2.name;
Go to Top of Page

kabon
Starting Member

48 Posts

Posted - 2013-03-05 : 23:59:39
okay thank you for your help james, i'll try it...
Go to Top of Page

kabon
Starting Member

48 Posts

Posted - 2013-03-06 : 01:56:57
can I use condition in this script?
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-03-06 : 05:27:46
Yes you can.

SELECT t1.*
--UPDATE t1 SET
-- Class = t2.Class,
-- Number = t2.Number
FROM
Table1 t1
INNER JOIN Table2 t2 ON
t1.name = t2.name
WHERE <place your condition(s) here>

Try first with SELECT and it pulls the required data.. then use udpate query .

Cheers
MIK
Go to Top of Page
   

- Advertisement -