I am trying to pass a query to Access using the RODBC package within R. I can import entire tables using sqlFetch, but I am looking to cherry pick data using a query.
However, I would like to use an R variable to select one of the constraints (Shaft_ID).
To construct the SQL query, I have used both Cat and Paste so I can use quotation marks around the Shaft variable.
My code is as follows:
Shaft<- "S001"
BL <- sqlQuery(myDBcon, paste("SELECT * FROM BL_FGSL WHERE Shaft_ID ='",Shaft,"'"), error = TRUE, as.is = TRUE)
This creates a data frame, but it does not find any values. If I replace the variable directly with "S001" it works!
Any ideas chaps?
catthere. Just omit it.catis only used for the side effect of writing to STDOUT (or a file); it returnsNULLrather than the printed text --is.null(cat(1:5, "\n")).' S001 '(with spaces on both sides) because you usedpaste()without specifying the separator: Either usepaste(<SQL string>, sep="")orpaste0(<SQL String>)