0

I have my ODBC query to connect to Teradata and just wonder if I can read in the SQL file as oppose to have SQL code in? I am trying to find R function for Python's pd.read_sql_query(f, con) where f is my SQL file with code.

So for my connection, it would change from:

con <- function(){
  

  query <- paste0("
                  SELECT * FROM table1
         
                  
                  ")
  
  print(queryData(query))
  
}

con<- data.frame(con())

to

con <- function(){
  

  query <- "SQL_code.sql"
  
  print(queryData(query))
  
}

con<- data.frame(con())
Kalenji
  • 401
  • 2
  • 19
  • 42

2 Answers2

0

read your sql from a file:

sql_query <- read.delim('/path/SQL_code.sql', header = FALSE) %>% as.character()

then define the connection and use it:

library(DBI)
db <- dbConnect(...)
dbGetQuery(db, sql_query)
flopeko
  • 149
  • 8
0

If I understand your question correctly, you could try something like this?

library(DBI)
library(readr)
df <- dbGetQuery(con, statement = read_file('SQL_code.sql'))
# con is your connection

If it does not solve your problem, there may be some solutions here: How to read the contents of an .sql file into an R script to run a query?

flafont11
  • 137
  • 7