views:

67

answers:

5

I have two tables, a patient table an and appointment table. I'm attempting to retrieve the information from both tables depending on what doctor is logged in at the time. My stored procedure is as follows:

ALTER PROCEDURE dbo.sprocGetAllAppointmentsForUser
@UserID varchar(50)
   AS
SELECT Appts.appt_id, Appts.patient_id, Appts.dr_id, Appts.appt_time, Appts.appt_reason, p.patient_id, 
p.patient_first_name, p.patient_last_name, p.patient_addr, p.patient_city, p.patient_zip,
p.patient_state, p.patient_phone, p.patient_healthcare, p.patient_diagnosis, p.patients_user_id
FROM Patients p INNER JOIN Appts
ON Appts.patient_id = p.patients_user_id
WHERE Appts.dr_id = @UserID
RETURN

That should get every appointment and patient information correct? Or am I over simplifying it. Heres some sample data:

Appointment Table:

appt_id        patient_id        dr_id
   1               467             101
   2               242             101
   3               784             210

Will only return the first row, even though there are two rows corresponding to dr_id 101.

+1  A: 

The syntax looks sound, but are you sure you didn't mean p.patient_id instead of p.patients_user_id in the JOIN condition? Hard to tell for sure without seeing a database diagram though.

Matti Virkkunen
I agree with Matti. If you are looking for appointments for a patient then dr_id is probably wrong. Also, if the appointment does not have a dr (dr_id) is null or the dr_id is different those values won't be returned.
Matt Spradley
An appointment will always have a doctor (required on front end). My goal is to find all the appointments for a doctor which is why i'm calling for the dr_id. patient_id is an auto-incrementing number, while patients_user_id is a foreign key referencing back to a user table
Ryan
+1  A: 

You'll get all the appointments (that have patients) for that doctor - you'll also get the patients in those appointments.

If an appointment doesn't have a patient, it will not appear.

If a patient doesn't have an appointment, it will not appear.

If a patient has 2 appointments with this doctor, the patient will appear twice.

If an appointment has 2 patients, the appointment will appear twice.

David B
One appointment can't have two patients as it's an auto-incrementing primary key. How can I add "if a doctor has two appointments it will get the appointment info and patient info" to that list.
Ryan
A: 

Just a rough guess

instead of Appts.patient_id = p.patients_user_id

try Appts.patient_id = p.patient_id

priyanka.sarkar
As mentioned in a previous answer, the way my database is designed is a little 'off'. patient_id and patients_user_id is correct.
Ryan
+1  A: 

Are you positive all the patient ids you are looikng for actually exist in the patient table? Do you get a different result set if you change the join around like this:

FROM Patients p 
RIGHT JOIN ApptsON Appts.patient_id = p.patients_user_id
WHERE Appts.dr_id = @UserID
HLGEM
+1  A: 

try investigating this in steps first

SELECT 
  *
FROM Appts
WHERE Appts.dr_id = 101

if that returns 2 rows the data is ok in your appts table

then add

SELECT
    a.patient_id
    ,p.*
FROM Appts A
LEFT JOIN Patients p 
    ON Appts.patient_id = p.patients_user_id
WHERE Appts.dr_id = 101

if you see null values returned in p.* part, then there is something wrong with your join condition, you may be joining on a wrong field. or the patient record for corresponding patient_id is missing in the Patients table

kristof
This worked great, thanks!
Ryan