简体   繁体   中英

SQL “where” clause failing with R JDBC HANA connection

I've had nothing but trouble connecting to my company's HANA db through R but finally had a breakthrough, however now my sql statement is failing in subsetting data using a "where" statement.

The following returns a data frame of 10 observations across 9 variables

# Fetch all results
rs <- dbSendQuery(jdbcConnection, 'SELECT TOP 10 
VISITTYPE,
ACCOUNT,
PLANNEDSTART,
PLANNEDEND,
EXECUTIONSTART,
EXECUTIONEND,
STATUS, 
SOURCE, 
ACCOUNT_NAME
                  FROM "_SYS_BIC"."cona-reporting.field-sales/Q_CA_R_SpringVisit"')

a <- dbFetch(rs)

However when I throw a where into it, I receive an error.

rs <- dbSendQuery(jdbcConnection, 'SELECT TOP 10 
VISITTYPE,
ACCOUNT,
PLANNEDSTART,
PLANNEDEND,
EXECUTIONSTART,
EXECUTIONEND,
STATUS, 
SOURCE, 
ACCOUNT_NAME
                  FROM "_SYS_BIC"."cona-reporting.field-sales/Q_CA_R_SpringVisit" WHERE VISITTYPE = ZR')

Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
  Unable to retrieve JDBC result set for SELECT TOP 10 
VISITTYPE,
ACCOUNT,
PLANNEDSTART,
PLANNEDEND,
EXECUTIONSTART,
EXECUTIONEND,
STATUS, 
SOURCE, 
ACCOUNT_NAME
                  FROM "_SYS_BIC"."cona-reporting.field-sales/Q_CA_R_SpringVisit" WHERE VISITTYPE = ZR (SAP DBTech JDBC: [260] (at 222): invalid column name: ZR: line 11 col 101 (at pos 222))

What does this mean? ZR is not a column, it is a value within the column. Tried placing ZR in quotes to no other effect.

My double and single quote syntax is based on this other question I've asked.

Issues connecting R to HANA db with many special characters

Never got it working with RODBC so tried JODBC.

Likely it is handling of quotes within an embedded quote-enclosed string, further complicated by the double quote symbols used in SQL for identifiers. However, consider parameterization (an industry best practice whenever running SQL in application layer such as R) to avoid the need of quote punctuation or concatenation. Like most JDBC APIs, RJDBC supports parameterization . Also note, dbGetQuery summarily equates to dbSendQuery + dbFetch :

sql <- 'SELECT TOP 10 VISITTYPE,
                      ACCOUNT,
                      PLANNEDSTART, 
                      PLANNEDEND,
                      EXECUTIONSTART,
                      EXECUTIONEND,
                      STATUS, 
                      SOURCE, 
                      ACCOUNT_NAME
         FROM "_SYS_BIC"."cona-reporting.field-sales/Q_CA_R_SpringVisit" 
         WHERE VISITTYPE = ?'

param <- 'ZR'

df <- dbGetQuery(jdbcConnection, sql, param)

To complete the previous answer (which is of course preferable as it uses bind variables ) here is described the *root cause** of the problem:

The use of a single quote in a single quoted string must be of course escaped

Contrary to the Oracle escaping using doubling the quote R uses backslash.

ie the proper usage is as follows:

> df <-  dbGetQuery(jdbcConnection, 
+ 'select * from "DUAL" where "DUMMY" = \'X\'') 
> df
  DUMMY
1     X

alternative way using double quoted string

> df <-  dbGetQuery(jdbcConnection, 
+ "select * from \"DUAL\" where \"DUMMY\" = 'X'") 
> df
  DUMMY
1     X

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