sphinx001
sphinx001

Reputation: 1

Import data from SQL Server to R

I'm trying to import data from SQL Server to R. I was given the server name, user name and password to the SQL Server database. I've installed RODBC in R. But I don't know how to write the odbcConnect search as I don't have the database on my computer and I only know the server name. What should I do in such a situation? Thanks!

Upvotes: 0

Views: 8199

Answers (1)

Waldi
Waldi

Reputation: 41210

This is a typical SQL Server connection using DBI+odbc packages :

library(DBI)
library(odbc)

conn <- DBI::dbConnect(
    odbc::odbc(),
    Driver = "SQL Server",
    Server = "ServerName",
    Database = "DatabaseName",
    uid = "UserName",
    pwd = "Password",
    options(connectionObserver = NULL)
  )

data <- dbGetQuery(conn, "SELECT * FROM ...")

DBI is recommended by R Studio, and faster than RODBC. Credits @r2evans for checking this.

Upvotes: 2

Related Questions