I am trying to update a SQL table using sqlSave function of RODBC package in R. Data is present in a data frame. When I try to run the command:
sqlSave(DBConn, dat=df, verbose=T, tablename='table', append=T)
I get the following error:
Query: INSERT INTO "table" ( "col1", "col2", "col3", "col4" ) VALUES ( ?,?,?,?,? )
sqlwrite returned
42000 -131 [Sybase][ODBC Driver][Sybase IQ]Syntax error near 'table' on line 1
[RODBC] ERROR: Could not SQLPrepare 'INSERT INTO "table" ( "col1", "col2", "col3", "col4" ) VALUES ( ?,?,?,?,? )'
What am I doing wrong here so that I am not getting the values in SQLQuery?
Thanks for any help in advance
EDIT 1 after @Gordon comment:
Error shows 5 placeholders but my data.frame has only 4 columns. I did dim(df) and got 4. Is it somehow related to row index of df?
EDIT 2
On doing the following:
sqlSave(DBConn, dat=df, verbose=T, tablename='table', append=T)
The error now I get is still the same with 4 placeholders instead but all values are still (?,?,?,?)
EDIT 3
I tried using sqlUpdate also
sqlUpdate(DBConn, dat=df, verbose=T, tablename='table')
Error that I now got is:
Query: UPDATE "table" SET "col2"=?, "col3"=?, "col4"=? WHERE "col1"=?
Error in sqlUpdate(DBConn, t, tablename = "table", verbose = T) :
42000 -131 [Sybase][ODBC Driver][Sybase IQ]Syntax error near 'table' on line 1[RODBC] ERROR: Could not SQLPrepare 'UPDATE "table" SET "col2"=?, "col3"=?, "col4"=? WHERE "col1"=?'
INSERT
statements, as demonstrated here. Unfortunately this is not as efficient as leveraging the DB's native bulk insert / prepared statement capabilities, whichsqlSave
would presumably be doing if it functioned correctly, but it's better than nothing I suppose. – ZinazinahSET NOCOUNT ON; INSERT INTO <actual target table> (<columns...>) SELECT * FROM <temporary table>; SET NOCOUNT OFF; DROP TABLE <temporary table>;
, or something of that nature. I haven't tested this but I would imagine it to be faster than individual inserts. – ZinazinahsqlSave
is written properly (WRT creating new tables), it should use prepared statements to create / populate the temporary table, and not execute individual inserts. I don't have access to a DB at the moment, but you would do something likesqlSave(DBConn, dat = df, tablename = '#temp_table', append = FALSE)
to upload the data, and then proceed as described in my previous comment. – Zinazinah