如何从 R 中的 SQL 导出数据

how to export data from SQL in R

我是 R 的初学者

我通过 ODBC 连接到 SQL 数据库:

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

我可以查看initdata。我想从 ourTable 导出数据并将其保存到 Excel。

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

如果我理解你的问题,你需要将initdata中的数据输出到一个excel文件中。我会在 R 中使用 openxlsx 包。

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

希望对您有所帮助。