I am trying to pass a .sql
file into DBI::dbGetQuery
, but I run into problems whenever there is comment in the first line. For example:
runs_fine.sql
SELECT * FROM mytable
does_not_work.sql
-- Some comment
SELECT * FROM mytable
Then in R:
library(odbc)
library(tidyverse)
con <- dbConnect(odbc(), "my_connection")
dbGetQuery(con, read_file("sql/runs_fine.sql")) # success
dbGetQuery(con, read_file("sql/does_not_work.sql")) # fails
Motivation: I would like to leverage the convenient sql preview function from RStudio
, but this requires the first line be the following for any .sql
file:
-- !preview conn=con
...which causes the dbGetQuery
to fail. I suppose a workaround would be to use some sort of read.file function that can skip lines or avoid comment lines.
NOTE: If a comment line is in the middle of a .sql
file, dbGetQuery
succeeds as expected and avoids the appropriate commented lines.