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

∥☆過路亽.° 提交于 2020-01-24 22:05:39

问题


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?


回答1:


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)"


来源:https://stackoverflow.com/questions/58610539/r-pass-required-variable-from-odbc-hana-connection-to-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!