Enterrador99
Enterrador99

Reputation: 131

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")

Upvotes: 0

Views: 2395

Answers (1)

Avi
Avi

Reputation: 1845

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

Upvotes: 1

Related Questions