简体   繁体   中英

What is the correct syntax of RODBC query in R

I'm trying to query an Access database using RODBC in R using the code below:

library(RODBC)
channel <- odbcConnectAccess("S:\\mypath\\PhysiPopDONOTTOUCH\\Physiology.mdb")

data <- sqlQuery( channel , paste ("select Endoscopy.*,
Histology.Diagnosis from Endoscopy JOIN PatientData ON
Endoscopy.HospNum_Id=PatientData.HospNum_Id  JOIN Histology ON
Histology.HospNum_Id=PatientData.HospNum_Id WHERE histology.VisitDate
= Endoscopy.VisitDate"))

. This is not successful as I get the error:

[1] "42000 -3506 [Microsoft][ODBC Microsoft Access Driver] Syntax
error in FROM clause."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select Endoscopy.*,
Histology.Diagnosis from Endoscopy JOIN PatientData ON
Endoscopy.HospNum_Id=PatientData.HospNum_Id  JOIN Histology ON
Histology.HospNum_Id=PatientData.HospNum_Id WHERE Histology.VisitDate
= Endoscopy.VisitDate'"

The tables Endoscopy and Histology are each one to many with the PatientData table. What am I doing wrong with the syntax?

MS Access SQL requires JOIN pairings to be enclosed in parentheses. Also, use of paste() is to eliminate long whitespaces inside string values that appear when you break lines in R. And consider using table aliases to concisely reference tables. Include a semicolon terminator too (see: When should I use semicolons in SQL Server? ). See below adjustment:

data <- sqlQuery(channel , 
                 paste("SELECT e.*, h.Diagnosis",
                       "(FROM PatientData p"
                       "INNER JOIN Endoscopy e p ON e.HospNum_Id = p.HospNum_Id)",
                       "INNER JOIN Histology h ON h.HospNum_Id = p.HospNum_Id",
                       "WHERE h.VisitDate = e.VisitDate;", sep=" "))     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM