简体   繁体   中英

R Pass required variable from ODBC/HANA connection to sql statement

I have a table I am trying to call with my usual method

sql <- 'SELECT TOP 10 *
         FROM "_SYS_BIC"."data-path.self-service.DOIP/table_name"'

df <- dbGetQuery(jdbcConnection, sql)

and receive the error

Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ",  : 
  Unable to retrieve JDBC result set for SELECT TOP 10 *
         FROM "_SYS_BIC"."data-path.self-service.DOIP/table_name" (SAP DBTech JDBC: [2048]: column store error: search table error:  [34023] Instantiation of calculation model failed;exception 306106: Undefined variable: $$IP_ExtractionWeekFrom$$. Variable is marked as required but not set in the query)

I've been trying to insert IP_ExtractionWeekFrom into the sql statement with a where clause with no luck

param1 <- 201943

sql <- 'SELECT TOP 10 *
         FROM "_SYS_BIC"."ccf-edw.self-service.DOIP/R_CA_B_DemandPlan" where
         "$$IP_ExtractionWeek$$" = ?'

SpringVisit <- dbGetQuery(jdbcConnection, sql, param1)

I've tried the term surrounded by the "$$" and without, and both with and without "$$" sourrounded in quotes and not. Usually am met with an "invalid column name" error.

Is this supposed to be called with something other than a where clause?

Consider maintaining your working Tableau query with the integration of parameters in R with properly handling of double quotes for identifiers and single quotes for literals.

Additionally, parameterization is not supported with the old ('PLACEHOLDER'= ('<varname>', <varvalue>)) syntax.

Instead, as explained in How to escape sql injection from HANA placeholder use the PLACEHOLDER."<varname>" =>? syntax.

param1 <- 201943

sql <- "SELECT TOP 10 * 
        FROM \"_SYS_BIC\".\"ccf-edw.self-service.DOIP/R_CA_B_DemandPlan\"( 
             PLACEHOLDER.\"$$IP_ExtractionWeekFrom$$\", ?), 
             PLACEHOLDER.\"$$IP_ExtractionWeekTo$$\",?) 
        )\"_SYS_BIC\".\"ccf-edw.self-service.DOIP/R_CA_B_DemandPlan\" 
        WHERE (1 <> 0)"

SpringVisit <- dbGetQuery(jdbcConnection, sql, param1, param1)

Additionally, if your JDBC already connects to the schema _SYS_BIC , use the synonymous qualifier :: as original query in order to reference package and calculation view:

sql <- "SELECT TOP 10 * 
        FROM \"ccf-edw.self-service.DOIP::R_CA_B_DemandPlan\"( 
             PLACEHOLDER.\"$$IP_ExtractionWeekFrom$$\", ?), 
             PLACEHOLDER.\"$$IP_ExtractionWeekTo$$\", ? ) 
        )\"ccf-edw.self-service.DOIP::R_CA_B_DemandPlan\" 
        WHERE (1 <> 0)"

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