[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Update from a select statement

Posted on 2009-04-17
3
Medium Priority
?
157 Views
Last Modified: 2013-11-05
I don't know why but this always trips me up.  I need to update tableA using the data from tableB.  How do I do that?
update tableA set field1 = 
	(select a.field2
	from tableA a, tableB b
	where b.field5 = a.field2
	and b.field3 = 13)

Open in new window

0
Comment
Question by:scross1276
3 Comments
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 24169080
Give this a try:
update tableA 
set field1 = a.field2
from tableA a, tableb b
where b.field5 = a.field2
and b.field3 = 13

Open in new window

0
 

Author Comment

by:scross1276
ID: 24169211
Ugh, so simple!  Thanks, it worked great!!
0
 
LVL 27

Expert Comment

by:Chris Luttrell
ID: 24169260
ralmada's should work, this is just in ANSI standard SQL with the JOIN.  I like this syntax better because it clearly defines the join conditions from the filter conditions in the WHERE clause.
update tableA 
set field1 = a.field2
from tableA a JOIN tableb b ON b.field5 = a.field2
where b.field3 = 13

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question