Skip to content
Advertisement

how to export data from SQL in R

I’m a beginner in R.

I have connection via ODBC to SQL DB:

dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;Server=OurServer; Database=OurDatabase;Uid=; Pwd=")
initdata <- sqlQuery(dbconnection,paste("exec ourTable"))`

I can view initdata. I would like to export data from ourTable and save it into Excel.

{I find many information but this information is about how to save Table into SQL}

Advertisement

Answer

If I understand your question, you need to output the data in initdata into an excel file. I would use the openxlsx package in R.

 library(openxlsx)

 dbconnection <- odbcDriverConnect("Driver=ODBC Driver 11 for SQL Server;
                                    Server=OurServer; 
                                    Database=OurDatabase;
                                    Uid=;
                                    Pwd=")

 initdata <- sqlQuery(dbconnection,paste("exec ourTable"))`     

  #output the xlsx file
 write.xlsx(initdata,file = "file_name.xlsx")

I hope this helps.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement