I am running a script in R and then need its results to be sent to SQL Server.
During the first run I used this code:
sqlSave(conn, df, tablename = "myschema.result_table", rownames=FALSE,
append=FALSE, varTypes=varTypes)
So the table was created and everything worked fine. Now I would like to update the results in a following way:
- if
Id
exists inresult_table
, update the contents. - if
Id
doesn't exist inresult_table
, append the row with thisId
.
The problem is, if I use:
sqlUpdate(conn, df, tablename = "myschema.result_table", index = "Id", fast = FALSE)
I get an error as soon as the script finds an Id
not existing in result_table
. When using sqlSave
with append=TRUE
, everything gets appended without further checks and I get double rows.
Is there any solution to this problem or do I have to create a new result table each time and later merge these tables in SQL?