简体   繁体   English

无法使用R中的sqlSave追加到SQL Server表

[英]Unable to append to SQL Server table using sqlSave in R

I am trying to update a SQL table using sqlSave function of RODBC package in R. Data is present in a data frame. 我正在尝试使用R中RODBC包的sqlSave函数更新SQL表。数据存在于数据框中。 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? 我在这里做错了什么,以至于我没有在SQLQuery中获取值?

Thanks for any help in advance 在此先感谢您的帮助

EDIT 1 after @Gordon comment: @Gordon评论后编辑1

Error shows 5 placeholders but my data.frame has only 4 columns. 错误显示5个占位符,但我的data.frame只有4列。 I did dim(df) and got 4. Is it somehow related to row index of df? 我做了dim(df)并得到4.它是否与df的行索引有某种关系?

EDIT 2 编辑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 (?,?,?,?) 我得到的错误仍然与4个占位符相同,但所有值仍然是(?,?,?,?)

EDIT 3 编辑3

I tried using sqlUpdate also 我也尝试过使用sqlUpdate

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

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 )

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' . 如果我从数据框中省略了这一列,它会给我这个错误, 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. 如果我将此列设为NA ,它将为我提供Invalid character value for cast specification我能够使用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. 要解决此问题,请从包含除主键之外的所有列的表创建VIEW,以便可以附加到此VIEW而不是表,然后您不需要附加主键。 in my case, the view is called "insert_view" 在我的例子中,视图称为“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)

Check using verbose=TRUE in sqlSave argument if any of the columnames in the insert query has the same that you have in your original table. 如果insert查询中的任何列名与原始表中的列名相同,请在sqlSave参数中使用verbose = TRUE进行检查。

In my table I used to have a columname with space (the same if it has numbers o different character). 在我的表中,我曾经有一个带空格的列名(如果它有不同字符的数字,则相同)。 It won´t work because sqlSave will remove those character when it creates the query. 它不起作用,因为sqlSave会在创建查询时删除这些字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM