Connect R to a SQL Server database engine

后端 未结 3 1131
遥遥无期
遥遥无期 2020-12-10 15:32

At my work I have R-Studio (Microsoft R Open 3.2.5) installed and would like to connect to a Microsoft SQL Server database, to run scripts using the tables I have.

3条回答
  •  难免孤独
    2020-12-10 16:13

    Something like this should work:

    library(RODBC)
    dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=YourDBName\\SQLEXPRESS; Database=TestDB;Uid=; Pwd=; trusted_connection=yes")
    initdata <- sqlQuery(dbconnection,paste("select * from MyTable;"))
    odbcClose(dbconnection)
    

    Check out these links:

    RODBC odbcDriverConnect() Connection Error

    https://andersspur.wordpress.com/2013/11/26/connect-r-to-sql-server-2012-and-14/

    Finally, make sure SQL Server has all proper permissions applied.

提交回复
热议问题