我在Server中创建了一个表,如下所示:
CREATE TABLE testPK
(
ID INT NOT NULL IDENTITY (1, 1) PRIMARY KEY,
NumVal NUMERIC (18, 4)
)
现在,我希望使用RODBC函数testPK将数据添加到R程序的sqlSave()
中,如下所示:
# 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表的该列上的标识规范会导致Server在追加新记录时生成唯一的值。我怎样才能从R中获得这个结果?
here上也有同样的问题,但没有被接受的解决方案。
发布于 2017-11-21 01:09:48
我无法使用sqlSave()
找到解决方案,所以我使用了概述here的方法,将任意数量的列附加到SQL表中。对于我的单列数据表,下面的代码达到了预期的结果:
# Specify data to append
test.dt <- data.table(NumVal = 1.0)
# Assign connection
myconn <- odbcDriverConnect(connectionString)
# Concatenate the VALUES portion of the query
values <- paste("(", test.dt$NumVal, ")", sep = "", collapse = ",")
# Create the full query
testQuery <- paste("INSERT INTO testPK (NumVal) VALUES", values)
# Append test.dt to SQL table testPK
sqlQuery(channel = myconn, query = testQuery)
# Close connection
odbcCloseAll()
https://stackoverflow.com/questions/47273104
复制相似问题