0

I am using this code to connect

library(dplyr)
library(DBI)


con <- dbConnect(odbc::odbc(), "SQLSERVERR") # Can't use this

con1 <- dbConnect(odbc::odbc(), "SQLSERVERR",database = "db1")
con2 <- dbConnect(odbc::odbc(), "SQLSERVERR",database = "db2")

tbx <- tbl(con1,"some_table")

But my connection has tables inside databases and I would like to send sql queries to them using dplyr.

something like

tbx <- tbl(con, db = "db1", "some_table")

Thanks for the help.

Bruno
  • 4,109
  • 1
  • 9
  • 27
  • This is very broad, and seems like you're looking more for a tutorial than help with a specific code question. There is pretty [extensive guidance](https://db.rstudio.com/dplyr/) I'd recommend starting with on databases with `dplyr` – camille Jul 11 '19 at 15:43

1 Answers1

0

It might depend on a few things. I had the best luck doing a similar task using the RODBC package. SQL Server RODBC Connection I could access views within the database this way using the sqlFetch function but it is possible to do the query within the sqlQuery function as shown in the example. If you're accessing a secure, live database, you may want to try the odbcConnect function (also within the same package), having added it as a datasource (https://turbofuture.com/computers/Connect-to-SQL-Server-from-R).

Rex Parsons
  • 199
  • 10