Unable to append to SQL Server table using sqlSave in R

12,495

Solution 1

There is a possibility of data types and Column names being a problem. So It's best to obtain the datatypes and column names of the table and assign them to the data frame.

ColumnsOfTable       <- sqlColumns(conn, tablename)
varTypes             <- as.character(ColumnsOfTable$TYPE_NAME) 
names(varTypes)      <- as.character(ColumnsOfTable$COLUMN_NAME) 
colnames(dataObject) <- as.character(ColumnsOfTable$COLUMN_NAME)

sqlSave(conn, dataObject, tableNames, fast=TRUE,append=TRUE,  rownames=FALSE, varTypes=varTypes )

Solution 2

The reason I had this problem is: I was trying to append to a table that had an auto-incrementing identity column. If I omitted this column from the data frame it would give me this error missing columns in 'data'. If I made this column NA it would give me Invalid character value for cast specification I was able to troubleshoot with verbose=TRUE. To solve, create a VIEW from the table that has all the columns except the primary key so you can append to this VIEW instead of the table, then you do not need to append the primary key. in my case, the view is called "insert_view"

var_Types <- as.character(as.character(c("int","int","varchar(50)","nvarchar(MAX)")))

names(var_Types) <- as.character(ColumnsOfTable$COLUMN_NAME)

sqlSave(ch, dataframe, "dbo.insert_view",rownames=FALSE,append = TRUE,varTypes=var_Types,verbose = TRUE)

Share:
12,495
user1465557
Author by

user1465557

Updated on June 08, 2022

Comments

  • user1465557
    user1465557 almost 2 years

    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"=?'