Select a list of variables in dbGetQuery

Hi All,

I want to use the following list of variables (38 variables ) : listv=list("var1","var2",var3","var4"...), to be selected inside dbGetQuery instead of calling each variable separetly with the select:
Result <- dbGetQuery(conn, paste0('select ', listv,' from Origin'))
But I get an error when trying the above.
Any ideas please?

Best Regards,
Hamza

Hello @hamza_rahal ,

you are rather vague about the error. Please be more specific in future postings.
I am sure you realize that the error is caused by the SQL select statement.
And you can see what it is by executing paste0('select ', listv,' from Origin') on its own.
So your question should have been: "how do I create a correct list of variables for the select" .
You can use the glue package for that (more easily than using base R only). See this :

 library(glue)
listv=list("var1","var2","var3","var4")
paste0('select ', listv,' from Origin') 
#> [1] "select var1 from Origin" "select var2 from Origin"
#> [3] "select var3 from Origin" "select var4 from Origin"
(listv2 <-glue::glue_collapse(listv,sep=", "))
#> var1, var2, var3, var4
glue::glue("'select {listv2} from Origin'")
#> 'select var1, var2, var3, var4 from Origin'
Created on 2021-12-01 by the reprex package (v2.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.