2

I am trying to execute a SQL Query through R to get the data from Access DB

Normal SQL statement works fine, but when it comes to like statement its throwing error

Below is code :

library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb,    *.accdb)};DBQ=C:/Users/ADMIN/Documents/R.accdb")
test = sqlQuery(channel ,paste('SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like "*slow*"'))

Error: [1] "07002 -3010 [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'SELECT R.ID, R.Template, R.WEDate FROM R WHERE (R.Template Like \"slow\")'

Is there a way to fix this.

12
  • 1
    Just a thought, try using % instead of *. Commented Sep 6, 2016 at 18:23
  • @joran Still the same error Commented Sep 6, 2016 at 18:25
  • 1
    Try reversing your use of double and single quotes (single quotes on the inside, double on the outside). Commented Sep 6, 2016 at 18:26
  • @joran it didn't work as well Commented Sep 6, 2016 at 18:30
  • Not sure, must be something having to do with Access, I run LIKE queries like that using RODBC to other dbs all the time. Commented Sep 6, 2016 at 18:32

1 Answer 1

2

Consider both of @joran's suggestions with single quote enclosing string literals AND using the ANSI-92 wildcard operator %. You would use asterisk, * (ANSI-89 mode) when running an internal query, namely inside the MSAccess.exe GUI program (which defaults to DAO) or if you connect externally to Access with DAO. Meanwhile, ADO connections uses the percent symbol which most external interfaces uses including RODBC.

I was able to reproduce your issue and both these remedies worked. Also, no need to use paste() as you are not concatenating any other object to query statement.

library(RODBC);
channel = odbcDriverConnect("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; 
                             DBQ=C:/Users/ADMIN/Documents/R.accdb")

test = sqlQuery(channel, 
                "SELECT R.ID, R.Template, R.WEDate FROM R WHERE R.Template Like '%slow%'")
Sign up to request clarification or add additional context in comments.

1 Comment

this combination works well , Thanks for the Idea bro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.