Import data from SQL Server to R

陌路散爱 提交于 2021-02-11 12:36:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!