0

Can I pass an r variable into a function for selecting a column from the database so that different columns are selected every time the variable in r changes:

father <- 'father'
myfun(father)

Function:

myfun <- function (parent)
{
query <-  paste("SELECT '$parent' from table1 where EXTRACT(YEAR FROM dob) 
between '",date1,"' and '",date2,"'",sep='')

connect1 <<- dbGetQuery (con, query )
connect1
}
2
  • 1
    @mt1022 We would also need to make sure that there are spaces between SELECT and parent. So maybe make the separator a single space. Commented Dec 24, 2017 at 13:27
  • 1
    @TimBiegeleisen and Prasinus, sorry I did not notice that the default sep was changed. Just copied it from OP's code. Commented Dec 24, 2017 at 13:38

1 Answer 1

1

It is not possible to have a table column be a parameter in a prepared statement. But I don't see anything stopping you from trying something along the lines of the following:

parent <- "father"
query <- paste0("SELECT ", parent, " FROM table1 WHERE YEAR(dob) BETWEEN '", date1, "' AND '", date2, "'", sep='')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.