【发布时间】:2017-11-13 20:35:28
【问题描述】:
我在 SQL Server 中创建了如下表:
CREATE TABLE testPK
(
ID INT NOT NULL IDENTITY (1, 1) PRIMARY KEY,
NumVal NUMERIC (18, 4)
)
现在我想使用 RODBC 函数 sqlSave() 将数据从 R 程序附加到 testPK,如下所示:
# Specify data to append
test.dt <- data.table(NumVal = 1.0)
# Assign connection
myconn <- odbcDriverConnect(connectionString)
# Append test.dt to SQL table testPK
sqlSave(channel = myconn, dat = test.dt, tablename = 'testPK',
rownames = FALSE, append = TRUE)
# Close connection
odbcCloseAll()
但是,这会返回错误消息
Error in odbcUpdate(channel, query, mydata, coldata[m, ], test = test, :
missing columns in 'data'
我没有为数据表中的列 ID 提供值,因为我假设我的 SQL 表的该列的 IDENTITY 规范会导致 SQL Server 在追加新记录时生成唯一值。我怎样才能从 R 中获得这个结果?
同样的问题已发布here,但没有被接受的解决方案。
【问题讨论】:
标签: sql-server r rodbc