Skip to content

Instantly share code, notes, and snippets.

@sephraim
Created July 6, 2023 22:36
Show Gist options
  • Save sephraim/122293e3e0297e24640076d24ce65541 to your computer and use it in GitHub Desktop.
Save sephraim/122293e3e0297e24640076d24ce65541 to your computer and use it in GitHub Desktop.
Connect to SQL Server with R
# REQUIREMENTS:
# 1.) Your computer must be connected to the same network as the SQL Server,
# likely either through VPN or VDI.
# 2.) You will need to have the SQL Server driver installed. Admins can install
# it directly, but non-admins should be able to install it indirectly by
# installing Azure Data Studio.
library(DBI)
library(odbc)
# Set up the SQL Server connection string parameters
sql.server <- "name_of_db_server" # <- change this
sql.database <- "name_of_db" # <- change this
sql.driver <- "SQL Server" # <- don't change this
# Establish the SQL Server connection
sql.con <- dbConnect(odbc::odbc(),
driver = sql.driver,
server = sql.server,
database = sql.database,
trusted_connection = "yes")
# Close the SQL Server connection
dbDisconnect(sql.con)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment