简体   繁体   中英

sqlQuery in R after connecting with an oracle database

I've connected with an oracle database and I'm running a sql query to view data in R. Code below for reference:

library(RODBC)
db = odbcConnect("RenataDB", uid="xxrenfin", pwd="XXRENFIN", believeNRows=FALSE)
sqlQuery(db, 'SELECT DEPOTNAME,YEAR_MONTH,YEAR_TRAN,SALES,LINE_OF_BUSINESS FROM tbl_mod_sales_trend_cata WHERE LINE_OF_BUSINESS = 'PHARMACEUTICALS')

I'm getting an error regarding the sql query which is as follows:

"[RODBC] ERROR: Could not SQLExecDirect 'SELECT DEPOTNAME,YEAR_MONTH,YEAR_TRAN,SALES,LINE_OF_BUSINESS FROM tbl_mod_sales_trend_cata WHERE LINE_OF_BUSINESS = PHARMACEUTICALS'"

Up front, I'm inferring the problem, since the code you provided is a syntax error in R, and should be erring with unexpected symbol . Instead, I'll guess that you have a quoting problem. The error suggests this, with:

LINE_OF_BUSINESS = PHARMACEUTICALS'"

Notice how there's no open-single-quote before the PHARMA ?

Try

sqlQuery(db, "SELECT DEPOTNAME,YEAR_MONTH,YEAR_TRAN,SALES,LINE_OF_BUSINESS FROM tbl_mod_sales_trend_cata WHERE LINE_OF_BUSINESS = 'PHARMACEUTICALS'")

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