Use a database connection to read from an existing SQL Server table with a SQL query.

db_read(con, query, pull_into_memory = TRUE)



An odbc database connection. Can be made using build_connection_string. Required.


A string, quoted, required. This sql query will be executed against the database you are connected to.


Logical, optional, defaults to TRUE. If FALSE, db_read will create a reference to the queried data rather than pulling into memory. Set to FALSE for very large tables.


A tibble of data or reference to the table.


Use pull_into_memory when working with large tables. Rather than returning the data into memory, this function will return a reference to the specified query. It will be executed only when needed, in a "lazy" style. Or, you can execute using the collect() function.

See also

build_connection_string importFrom dbplyr as.sql


if (FALSE) { my_con <- build_connection_string(server = "HPHI-EDWDEV") con <- DBI::dbConnect(odbc::odbc(), .connection_string = my_con) d <- db_read(con, "SELECT TOP 10 * FROM [Shared].[Cost].[FacilityAccountCost]") # Get a reference and collect later ref <- db_read(con, "SELECT TOP 10 * FROM [Shared].[Cost].[FacilityAccountCost]", pull_into_memory = FALSE) d <- collect(ref) }