Kamil Skála
Kamil Skála

Reputation: 13

Parameter IN SQL in R

could you please help me with changing parameters in sql query in R studio

it could be something like this

target <- "POJ"

FPV_MREL_POJ <- dbGetQuery(con, "select LINE_REF_COLL from DM_IFRS.FPV_MREL_",target[1]")

but it doesnot work

Upvotes: 0

Views: 54

Answers (1)

Jakub.Novotny
Jakub.Novotny

Reputation: 3047

You may have forgotten to use paste0 function in your code.

target <- "POJ"

sql_query <- paste0("select LINE_REF_COLL from DM_IFRS.FPV_MREL_", target[1])

FPV_MREL_POJ <- dbGetQuery(con, sql_query)

EDIT: following code does both what OP asks for, and tries to prevent code injection as pointed out by Sirius in the comments:

library(glue)

con <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")

target <- "POJ"

tbl_name <- paste0("FPV_MREL_", target[1])

sql_query <- glue_sql("select LINE_REF_COLL from DM_IFRS.{tbl_name}", .con =  con)

FPV_MREL_POJ <- dbGetQuery(con, sql_query)

Upvotes: 1

Related Questions