AnaS90 AnaS90 - 11 months ago 91
R Question

How to export vector results as a data.frame from R to pré-existing Table on SQL Server

I have 3 vectors that contain the results of Test 1, 2 and 3. I would like to convert them to data.frames in order to be able export them to the SQL Server Results Table below.

How can I convert the vectors to a data.frame and export to SQL Server?

Table Results

Thanks in advance.

Answer Source

I have done this before. Here is the code I ended up using which writes data from a data.table in R to a TABLE in SQL Server 2012 Express:

You need to make use of the RODBC package. You will also need to setup a User DSN ODBC Connection. I called mine "LOCALHOST".


connection <- RODBC::odbcConnect("LOCALHOST")

try(RODBC::sqlDrop(connection, "dbo.iris", errors = FALSE), silent = TRUE)

sqlSave(connection, iris)