R ODCB / DBI使用非默认架构写入SQL Server表

时间:2018-03-08 21:41:14

标签: r sql-server database

我对dbWriteTable中的新架构行为感到非常兴奋,但却无法让它发挥作用。我正在运行DBI 0.7-15和SQL Server 2012.也许我只是错误地使用了Id功能?

以下代码块无法找到正确的方法。

cs <- "driver={SQL Server};
       server={localhost};
       database=testSAM;
       trusted_connection=true;"

con <- DBI::dbConnect(odbc::odbc(), .connection_string = cs)

df <- data.frame(id=1, word_of_day = "happy")
table_id <- DBI::Id(name = "hcai_unit_tests", 
                    schema = "dbo", 
                    catalog = "testSAM")


# Try with Id
res <- DBI::dbWriteTable(conn = con,
                         name = table_id,
                         value = df,
                         append = TRUE)

# Errors with:
Error in (function (classes, fdef, mtable)  : 
unable to find an inherited method for function ‘dbWriteTable’ for 
signature ‘"Microsoft SQL Server", "SQL", "missing"’

尝试使用dbQuoteIdentifier会产生不同的错误

res <- odbc::dbWriteTable(conn = con,
                          name = DBI::dbQuoteIdentifier(con, t),
                          value = df,
                          append = TRUE)

# Errors with:
Error: <SQL> 'CREATE TABLE "testSAM"."dbo"."hcai_unit_tests" (
  "id" FLOAT,
  "word_of_day" varchar(255)
)
'
nanodbc/nanodbc.cpp:1587: 42S01: [Microsoft][ODBC SQL Server Driver]
[SQL Server]There is already an object named
'hcai_unit_tests' in the database. 

在这里,append=TRUE应该阻止此错误出现......

0 个答案:

没有答案