How to Import Data into MS SQL Server via R? (Part 1: csv, Excel, Snowflake)
Manipulating the data resides in multiple platforms plays fundamental role when you do the data analysis, machine learning, reporting, etc. We are going to discuss multiple approaches of leveraging R to upload the data in csv file, MS Excel, Snowflake into MS SQL Server.
csv into MS SQL Server
- Required package: RODBC
- Steps:
Step 0: Set up MS SQL server in ODBC Data Source Admin.
Step 1: Define SQL Server connection.
Step 2: Set working directory and Read the csv file as a data frame df.
Step 3: Import the df into target_laptop_price_csv via sqlSave function.
Step 4: Close the connection.
library(RODBC)
setwd('H:/Tutorial')
conn <- odbcDriverConnect('driver={SQL Server};server=Your Server;database=Your DB;trusted_connection=true')
df <- read.csv("target_laptop_price.csv")
sqlSave(conn, df, tablename="target_laptop_price_csv", rownames=FALSE)
odbcClose(conn)
MS Excel into MS SQL Server
- Required packages: RODBC/readxl
- Steps:
Step 0: Set up MS SQL server in ODBC Data Source Admin.