【问题标题】:Write dataframe to table in Teradata from R using RODBC sqlSave使用 RODBC sqlSave 从 R 将数据帧写入 Teradata 中的表
【发布时间】:2019-10-28 07:10:23
【问题描述】:

这是运行数月的遗留 R 代码。我能够从 R 读取/删除 Teradata 中的表,但无法从数据帧将数据写入表中。

我尝试过删除表并重新创建和写入不同的数据框。

sqlSave(ch, df, tablename = paste("scenario.table_storetype"),rownames=F)

我收到以下错误

sqlColumns(channel, tablename)[4L][, 1L] 中的错误:错误的数量 尺寸追溯:
1. sqlSave(ch, df, tablename = paste("scenario.table_storetype"), .rownames = F, 更安全 = 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("准备好的沙拉、水果和蔬菜", “准备沙拉,水果和蔬菜”,“准备沙拉,水果和蔬菜”, “准备沙拉,水果和蔬菜”,“准备沙拉,水果和蔬菜”, “准备沙拉、水果和蔬菜”),周期 = 201904:201909,如何 = c(“a_tslm_mape”, “a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape” )), .Names = c("forecast", "actual", "BC", "period", "how"), 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("准备好的沙拉、水果和蔬菜", “准备沙拉,水果和蔬菜”,“准备沙拉,水果和蔬菜”, “准备沙拉,水果和蔬菜”,“准备沙拉,水果和蔬菜”, “准备沙拉、水果和蔬菜”),周期 = 201904:201909,如何 = c(“a_tslm_mape”, “a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape”、“a_tslm_mape” )), .Names = c("forecast", "actual", "BC", "period", "how"), row.names = c(NA, 6L), class= "data.frame")

【问题讨论】:

  • 您的 df 在列和行中有多大:dim(df)?尝试减少行并进行测试。 ODBC 驱动程序可能会施加限制。
  • @Parfait 我尝试写单行但没有帮助。我能够使用 sqlQuery 命令手动提供值插入一行,但 sqlSave 不起作用
  • 你能回答我的第一个问题吗?通过减少行,我的意思是保存前几行:sqlSave(ch, head(df, 10), tablename = paste("scenario.table_storetype"),rownames=F)。您的问题可能是尺寸限制。
  • @Parfait df 的维度是 [396, 5]
  • 您在 data.frame 中是否有嵌套对象,例如列表?请发布dput(head(df)),以便我们查看其属性/尺寸。

标签: r teradata


【解决方案1】:

显然,您的数字非常大,精度也各不相同。动态创建表时,数字字段可能映射为 DECIMAL(n, m) 类型,其中 2 的比例可以由第一个值设置,36659805.75 但这后来与比例为 7 的 33650734.4166667 冲突。

考虑使用sqlSavevarTypes 参数来显式定义数据类型。根据文档:

varTypes:一个可选的命名字符向量,如果要创建一个表,则给出用于部分(或全部)列的 DBMS 数据类型

此外,在 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)

【讨论】:

  • 我尝试四舍五入并将 varTypes 参数提供给 sqlSave 但没有帮助
  • 同样的错误?尝试这个。将几列发送到sqlSave 以查看哪个失败:sqlSave(ch, df[c("BC", "how")], ...)(varchar 字段)、sqlSave(ch, df[c("period")], ...)(int 字段)、sqlSave(ch, df[c("forecast", "actual")], ...)(数字字段)。
  • 抱歉@Parfait 回复晚了。但似乎没有任何效果
猜你喜欢
  • 2017-03-29
  • 2015-01-02
  • 1970-01-01
  • 2019-01-05
  • 2019-09-27
  • 1970-01-01
  • 2019-06-22
  • 2014-07-17
  • 2017-09-03
相关资源
最近更新 更多