这是运行了几个月的遗留 R 代码。我能够从 R 中读取/删除 Teradata 中的表,但无法将数据从数据框写入表中。
我尝试过删除表并重新创建和编写不同的数据框。
sqlSave(ch, df, tablename = paste("scenario.table_storetype"),rownames=F)
我遇到了以下错误
Error in sqlColumns(channel, tablename)[4L][, 1L]: incorrect number of dimensions Traceback:
1. sqlSave(ch, df, tablename = paste("scenario.table_storetype"), . rownames = F, safer = FALSE, append = T)
2. sqlwrite(channel, tablename, dat, verbose = verbose, fast = fast, . test = test, nastring = nastring)
dput(head(df))
输出: 结构(列表(预测= c(36659805.75,28117111.75,27005618.75, 33650734.4166667, 27243750.75, 26907919.0833333), 实际 = c(38293943, 29892143, 27016674, 33524728, 27252399, 26521098), BC = c("Prepared Salad, Fruit & Veg", “预制沙拉、水果和蔬菜”、“预制沙拉、水果和蔬菜”、 “预制沙拉、水果和蔬菜”、“预制沙拉、水果和蔬菜”、 “准备好的沙拉、水果和蔬菜”), period = 201904:201909, how = c("a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape" )), .Names = c("预测", "实际", "BC", "期间", "如何"), row.names = c(NA, 6L), class = "data.frame")
dput(head(df))
四舍五入后 - 输出: 结构(列表(预测= c(36659805.75,28117111.75,27005618.75, 33650734.42, 27243750.75, 26907919.08), 实际 = c(38293943, 29892143, 27016674, 33524728, 27252399, 26521098), BC = c("Prepared Salad, Fruit & Veg", “预制沙拉、水果和蔬菜”、“预制沙拉、水果和蔬菜”、 “预制沙拉、水果和蔬菜”、“预制沙拉、水果和蔬菜”、 “准备好的沙拉、水果和蔬菜”), period = 201904:201909, how = c("a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape", "a_tslm_mape" )), .Names = c("预测", "实际", "BC", "期间", "如何"), row.names = c(NA, 6L), class = "data.frame")
最佳答案
显然,您的数字非常大,但精度各不相同。动态创建表时,数字字段可能映射为 DECIMAL(n, m)
2 的比例可以由第一个值 36659805.75
设置的类型,但这后来与 33650734.4166667
的比例为 7 发生冲突。
考虑使用 sqlSave
的 varTypes 参数显式定义数据类型。根据文档:
varTypes: an optional named character vector giving the DBMSs datatypes to be used for some (or all) of the columns if a table is to be created
此外,在 R 中进行相应舍入以符合 Teradata 列类型的精度需求
df$forecast <- round(df$forecast, 4) # PRECISION OF 4
max(nchar(df$BC)) # FIND MAX CHARS
max(nchar(df$how)) # FIND MAX CHARS
tbl_types = c(forecast = "DECIMAL(12, 4)",
actual = "DECIMAL(12, 4)",
BC = "VARCHAR(50)",
period = "INTEGER",
how = "VARCHAR(20)")
sqlSave(ch, df, tablename = paste("scenario.table_storetype"),
varTypes = tbl_types, rownames = FALSE)
关于r - 使用 RODBC sqlSave 将数据框从 R 写入 Teradata 中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56584748/