问题
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!
回答1:
This is a typical SQL Server connection using DBI
+odbc
packages :
library(DBI)
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.
来源:https://stackoverflow.com/questions/63420459/import-data-from-sql-server-to-r