简体   繁体   中英

Convert table from Microsoft SQL Server to dataframe in R

I am working on a project and would like to connect to a database on MSSQL Server and then get one(or more) of the tables on the database and convert it/them to a dataframe to work in memory, then I will apply some changes to this dataframe and send it back to the MSSQL Server Database.

I am able to connect to my database and I know how to send a dataframe to a database but I don't know how to convert the table to a dataframe as the first step.

 con <- dbConnect(odbc(), Driver = "SQL Server", Server = "MyServer", Database = "MyDB", Trusted_Connection = "True") 

You can use SQLdf and SQL query as well to get your desired output (to get the data from a database and convert into a dataframe in R)

library(odbc)
library(RODBC)
library(sqldf)

conn <- odbcDriverConnect('driver={SQL Server};server= 
YOURserver;database=Yourdatabase;trusted_connection=true')

DataSQL <- sqlQuery(conn,"SELECT *  FROM dbo.practicR;"); 

View(DataSQL) 

Output: This is the same data in SQL server.

Output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM