views:

28

answers:

2

Not that good with SQL yet but I'm learning.

So I have 3 tables:

tblOne(Id, Type) tblTemp1(Name, Type) tblTemp2(Id, Name)

Basically, I want to update tblOne where the where it's 'Id' matches from tblTemp2, but than that also goes and grabs its 'Type' from tblTemp1 where the 'Name' matches.

Can anyone help?

+2  A: 

I would suggest using joins in your update statement.

UPDATE tblOne 
SET tblOne.Type = tblTemp1.Type
FROM tblTemp2
INNER JOIN tblTemp1 ON tblTemp1.Name = tblTemp2.Name
WHERE tblOne.Id = tblTemp2.Id

http://msdn.microsoft.com/en-us/library/aa260662%28SQL.80%29.aspx

Victor Nicollet
Is this syntax correct? I've never seen a JOIN performed before a set? (Not picking holes, just never seen this before)
Ardman
Yeah can you do that?
Scott
You're right. This is a MySQL-specific syntax. I've edited the post to use the Sql Server syntax.
Victor Nicollet
A: 
UPDATE tblOne
SET
tblOne.Type = tblTemp1.Type
WHERE
tblOne.Id = tblTemp2.Id
AND
tblTemp1.Name = tblTemp2.Name;
Leniel Macaferi