Unable to append to SQL Server table using sqlSave in R

前端 未结 3 1184
灰色年华
灰色年华 2021-01-13 05:40

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(DBCon         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 06:14

    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)

提交回复
热议问题