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
 General SQL Server Forums
 New to SQL Server Programming
 Having afew issues with a join!!

Author  Topic 

Caz1224
Starting Member

2 Posts

Posted - 2009-09-13 : 18:45:26
Hi there,
The line I am trying to run is to combine 3 tables on a primary key of "patient_id"
The three tables are
patient
pat_details
rec_location

Below is my code...

SELECT * FROM pat_details INNER JOIN patient
ON pat_details.patient_id = patient.patient_id
JOIN pat_details
ON pat_details.patient_id = rec_location.patient_id

And the error:
Msg 1013, Level 16, State 1, Line 1
The objects "pat_details" and "pat_details" in the FROM clause have the same exposed names. Use correlation names to distinguish them.


Help!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-13 : 23:36:43
Your second join has pat_details repeated, which it doesn't sound like you want to do. If you do want to join to pat_details twice, then you'll need to alias them (hence the error).

Here is probably what you want:
SELECT *
FROM pat_details
JOIN patient
ON pat_details.patient_id = patient.patient_id
JOIN rec_location
ON pat_details.patient_id = rec_location.patient_id

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Caz1224
Starting Member

2 Posts

Posted - 2009-09-15 : 23:41:11
Thank you soooo muchly that worked a treat!
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-16 : 13:13:43
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-09-17 : 02:11:25
Also explicitely type the column names as there is high chance of having duplicate columns when you use *

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -