Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update using inner joins and there tables 1

Status
Not open for further replies.

nobeltlp

Programmer
Aug 22, 2005
6
0
0
US
Im very new on Sql server.

Here are two examples that seem to both work but Im not to sure.
Could someone evaluate these statements for me.
I would be very appreciative for the help.

Example1. this seems to update correctly
Code:
Update TBL-1
Set TBL-1.txtName = TBL-2.txtName
From TBL-2 INNER JOIN
	TBL-3 ON TBL-2.txtID=TBL-3.txtID
Where  TBL-1.txtACT = TBL-3.txtACT 
AND TBL-3.txtR= ‘P’

Example2 This also seems to update correctly.
The red is where I really got lost as TBL-2 doesn't have a
txtACT field but TBL-3 has A txtID field.
Code:
UPDATE    TBL-1
SET      TBL-1.txtN = TBL-2.txtN
FROM     TBL-2 INNER JOIN
         TBL-1 INNER JOIN
         TBL-3 ON TBL-1.txtACT = TBL-3.txtACT
[COLOR=red]  AND 
  TBL-1.txtACT = TBL-3.txtACT ON TBL-2.txtID = TBL-3.txtID[/color]
WHERE  tblRelationships.txtRelationship = 'P'
 
I believe this is how you would join your tables:

Code:
UPDATE   [TBL-1]
SET      [TBL-1].txtN = [TBL-2].txtN
FROM     [TBL-1] 
	   INNER JOIN [TBL-3] ON [TBL-1].txtACT = [TBL-3].txtACT 
	   INNER JOIN [TBL-2] ON [TBL-2].txtID = [TBL-3].txtID
WHERE tblRelationships.txtRelationship = 'P'

But if you don't have tblRelationship somewhere in the FROM statement, it will fail because it cannot find the field in the WHERE clause.

Also, I'm assuming these tablename are bogus for example purposes. If not, stay away from hyphens in tablenames otherwise you have to qualify the table with brackets each time it's referenced.

Hope this helps
Andy
 
Is tblRelationships.txtRelationship the same as TBL-3.txtR or a separate table?
 
If Im responding correctly.
To: Ankor Yes, TBL-3 is tblRelationships, Sorry to All
for that.

To: jabrony76
Thank you for the proper inner join of the tables,
I used you example and the update was correct.

Query Analyzer is not my friend.
Does it alway say this;
"The Query Designer does not support the Optional FROM clause SQL construct."
When using From?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top