[short] Worked example on setting up SQL Server with R ODBC connection

[This article was first published on R on The Stats Guy, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This is a worked example on how to set up SQL Server, SQL Server Management Studio, and a ODBC connection with R.

Step 1: Install SQL Server from https://www.microsoft.com/en-us/sql-server/sql-server-downloads. The SQL Server 2017 Express was good enough for me to run some analysis and modelling on my own. Once done, you should have a screen like this:

Step 2: Click on the “Install SSMS” button. SSMS stands for SQL Server Management Studio. Once done, connect to the server:

Step 3: Create a database on the server. You may follow the steps given in this page as a quick start: https://docs.microsoft.com/en-us/sql/ssms/tutorials/connect-query-sql-server?view=sql-server-2017. If you do, you should have a database created named “TutorialDB” and a table named “Customers”.

Step 4: Install and load the RODBC package in R.

#install.packages("RODBC")
library(RODBC)

Step 5: Connect to the server and the database, and run a sample query.

conn <- odbcDriverConnect('driver={SQL Server};server=SNG1049387\\SQLEXPRESS;database=TutorialDB;trusted_connection=true')
customers <- sqlQuery(conn, 'select * from dbo.Customers')
str(customers)
# 'data.frame': 4 obs. of  4 variables:
#  $ CustomerId: int  1 2 3 4
#  $ Name      : Factor w/ 4 levels "Donna","Janet",..: 4 3 1 2
#  $ Location  : Factor w/ 4 levels "Australia","Germany",..: 1 3 2 4
#  $ Email     : Factor w/ 4 levels "","[email protected]",..: 1 4 2 3

Step 6: Write an R data frame into your database.

df <- read.csv("data/adult.csv")
sqlSave(conn, df)

Step 7: Refresh the Database node in SMSS to verify if the data frame has been written into the database as a table.

You are now ready to use SQL Server, SSMS, and R to run some analysis and modelling.

To leave a comment for the author, please follow the link and comment on their blog: R on The Stats Guy.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)