简体   繁体   English

R RODBC sqlSave与自动递增列在表中

[英]R RODBC sqlSave in table with Auto Incrementing Column

I am having a table "Table1" in a database with the columns AiID and Name, where AiID is the primary key and an auto-incrementing integer and Name any character. 我在数据库AiID和Name列中有一个表“ Table1”,其中AiID是主键和一个自动递增的整数,并命名任何字符。

When I want to use sqlSave : 当我想使用sqlSave

df <- data.frame(AiID = as.integer(NA), Name = as.character("Test"))
sqlSave(channel, dat = df, tablename = "dbo.Table1", append = T, rownames = F, fast = F, test = F)

I get the error: "Error in sqlSave(channel, dat = df, : unable to append to table 'dbo.Table1'" 我收到错误:“ sqlSave(channel,dat = df,中的错误:无法追加到表'dbo.Table1'”)

My feeling is that it has to do with the auto-incrementing AiID, but how would I deal with that in R/RODBC, when I want to insert new rows? 我的感觉是它与自动递增的AiID有关,但是当我要插入新行时,如何在R / RODBC中处理呢?

The channel itself is working, as I can update existing entries. 频道本身正在运行,因为我可以更新现有条目。

One possible answer for the error is your column definition of AiID. 该错误的一个可能答案是AiID的列定义。 If it is function generated in SQL, it may not let you input any given value. 如果它是用SQL生成的函数,则可能不允许您输入任何给定值。 This happened to me ONLY when I defined a function generated column as Primary Key. 仅当我将函数生成的列定义为主键时,这才发生在我身上。 I was able to append my values by copying the PK column values into a new column, defining the new column as a PK and generating PK values from R. In my case it was a concatenation of columns, in your case the autoincremental. 通过将PK列值复制到新列中,将新列定义为PK并从R中生成PK值,可以附加我的值。在我的情况下,这是列的串联,在您的情况下是自动递增。 RODBC don't let you write a subset of columns (as far as I know). RODBC不允许您编写列的子集(据我所知)。

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

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