15

I have this very simple sql statement:

SELECT     max_dose
FROM         psychotropes
WHERE     (patient_meds.psychotrope = psychotrope_name) AND (patient_meds.patient_id = 12)

when I try to run it in Visual Studio 2008, it tells me "The multi-part 'patient_meds.psychotrope' identifier could not be bound"

it's weird, because I did set a relationship between the two tables in the diagram viewer

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
jello
  • 745
  • 5
  • 12
  • 21

2 Answers2

16

I guess you'll have to include patient_meds in the table list as:

FROM psychotropes, patient_meds
codaddict
  • 445,704
  • 82
  • 492
  • 529
9

You are not including the table in the query. Without knowing the schema this is just an assumption. Also a database diagram does nothing to assist in queries.

SELECT ax_dose
FROM psychotropes
INNER JOIN patient_meds ON psychotropes.psychotrope_name = patient_meds.psychotrope
WHERE (patient_meds.patient_id = 12)
Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
  • "Also a database diagram does nothing to assist in queries" you mean I don't need to link two tables together with a foreign key to join them? – jello Mar 28 '10 at 05:06
  • 2
    @jello: Technically, in most RDBMSs, no. It's still a good idea to, though. – Michael Petrotta Mar 28 '10 at 05:11
  • @durilai: An ERD saves having to run multiple `DESC table` commands to get the same info. – OMG Ponies Mar 28 '10 at 05:17
  • @OMG, I understand the benefits. My thoughts were that he was assuming that since he had defined a foreign key he did not have to manually join the table. @Michael, Agree. FK constraints should be included where appropriate. – Dustin Laine Mar 28 '10 at 14:37