AnaS90 AnaS90 - 1 month ago 22
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

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

addPackage("RODBC")

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

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

sqlSave(connection, iris)

RODBC::odbcClose(connection)