当数据库表名称中不包含下划线时,dbwritetable()会出错

时间:2019-06-02 04:06:07

标签: r

我正在尝试将数据帧加载到SQL Server数据库表中。该表可能处于默认“ dbo”架构中,也可能不在默认架构中。当表名不包含至少一(1)个“下划线”时,该语句将出错。当表名确实包含“下划线”时,相同的代码可以完美地工作。

这是我执行的语句:-

table_id <- Id(schema = "dbo", name = "STGasFlatLoadAdjustments")
res <- dbWriteTable(conn = dbi_pfdb, name = table_id, value = testdf, append = TRUE)

使用DBI软件包报告了类似问题的人在线上看到了很多评论。

软件包中是否存在已知错误或该错误的已知解决方法?

这些命令在相同的源数据帧和相同的目标数据库表上有效,而不能一起使用-唯一的区别是表名。

dbi_pfdb <- DBI::dbConnect(odbc::odbc(), .connection_string = "Driver={SQL Server}; server=DEEPAKPC; database=MyDB; trustedConnection=true;")

这是源数据(用于创建数据框或用于创建数据框的csv文件)

Entity,MIRN,FromDate,ToDate,AdjustmentsValue,DerivedFlatLoadSite,UserSpecifiedFlatLoadSite,Comments,LoadDate
NSW_TRU,5240433621,2019-01-01 00:00:00.000,2020-12-31 00:00:00.000,4500.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-01-05 00:00:00.000,2019-01-05 00:00:00.000,-4500.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-01-06 00:00:00.000,2019-01-06 00:00:00.000,-4500.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-01-05 00:00:00.000,2019-01-05 00:00:00.000,3800.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-01-06 00:00:00.000,2019-01-06 00:00:00.000,3900.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,0000000000,2019-05-20 00:00:00.000,2019-05-20 00:00:00.000,-4200.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-05-18 00:00:00.000,2019-05-18 00:00:00.000,-4500.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-05-19 00:00:00.000,2019-05-19 00:00:00.000,-4500.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-05-18 00:00:00.000,2019-05-18 00:00:00.000,3800.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000
NSW_TRU,5240433621,2019-05-19 00:00:00.000,2019-05-19 00:00:00.000,3900.0000000000,1,1,TTest Comment,2019-05-15 00:00:00.000

用于为要上传到其中的源数据创建SQL Server表的代码。

USE [MyDB]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[STGas_FlatLoadAdjustments](
    [Entity] [varchar](50) NULL,
    [MIRN] [varchar](50) NULL,
    [FromDate] [datetime] NULL,
    [ToDate] [datetime] NULL,
    [AdjustmentsValue] [numeric](18, 10) NULL,
    [DerivedFlatLoadSite] [int] NULL,
    [UserSpecifiedFlatLoadSite] [int] NULL,
    [Comments] [varchar](max) NULL,
    [LoadDate] [datetime] NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

表名不包含任何下划线,这是我得到的错误:-

Error in connection_sql_tables(conn@ptr, catalog_name = if ("catalog" %in%  :
  nanodbc/nanodbc.cpp:2525: 24000: [Microsoft][ODBC SQL Server Driver]Invalid cursor state

0 个答案:

没有答案