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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 How to update table
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kabon
Starting Member

Indonesia
48 Posts

Posted - 03/05/2013 :  23:42:41  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3873 Posts

Posted - 03/05/2013 :  23:49:12  Show Profile  Reply with Quote
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

Indonesia
48 Posts

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

kabon
Starting Member

Indonesia
48 Posts

Posted - 03/06/2013 :  01:56:57  Show Profile  Reply with Quote
can I use condition in this script?
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 03/06/2013 :  05:27:46  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000