I know I can create a temporary table in SQL from R with, for example:
require(RODBC)
X<- odbcDriverConnect('driver={SQL Server};
server=s001000;database=X1;trusted_connection=true')
sqlQuery(X, "create table #temptable (test int)" )
sqlQuery(X, "insert into #temptable(test) values(201508)")
doesItWork <- sqlQuery(X, "select * from #temptable")
But I would like to create a temporary table in sql server from an R object (I have a table that has the result of previous R calculations and I need to query it against another table in SQL. I don't want to export it as txt and upload it to SQL server. It has to be a way to do it from R. I tried:
tabla<-data.frame(per=c(201508,201510))
sqlQuery(X, "Select * into ##temporal from tabla")
But I got an error message:
"42S02 208 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name 'tabla'." "[RODBC] ERROR: Could not SQLExecDirect 'Select * into ##temporal from tabla '"
I also know I can create a table with sqlSave:
sqlSave(X, tabla, rownames=FALSE,safer=FALSE)
But I want to create a temporary table. How can I create a temporary table in SQL from an R object?
tabla
) within a query viasqlQuery
as you can with, e.g.sqldf
. Try usingsqlSave
orsqlUpdate
for your insert operation. – Adz