gpt4 book ai didi

r - 尝试将表写入非默认模式时 SQL 函数不起作用

转载 作者:行者123 更新时间:2023-12-05 05:11:49 24 4
gpt4 key购买 nike

我正在尝试将表写入 SQL Server 2017 中的非默认架构。我正在使用 RStudio 的文档来了解执行此操作的最佳做​​法:https://db.rstudio.com/best-practices/schema/#write-non-temporary-tables .

但是,当我遵循他们的指南时,我收到以下错误:Error: Can't unquote EXISTING_SCHEMA.newTbleIris

编辑

经过一番挖掘,我取得了一些进展。但问题并没有完全解决。我发现 Id() 函数允许我读取但不能写入。下面的例子。我在尝试编写时遇到的错误是:

connection_sql_tables(conn@ptr, catalog_name = if ("catalog"%in% : nanodbc/nanodbc.cpp:2525: 24000: [Microsoft][ODBC SQL Server Driver]游标状态无效

新的可重现示例

library(odbc)
library(dbplyr)
library(dplyr)
library(DBI)

con <- dbConnect(odbc(),
Driver = "SQL SERVER",
Server = "SERVER_NAME",
Database = "DB_NAME",
UID = "USER_NAME",
PWD = "PASSWORD")

# Works assuming you already have iris table in EXISTING SCHEMA.
# I did this not in R because I cannot currently write to a non-default schema in R.
dbReadTable(con, Id(schema = "EXISTING_SCHEMA", name = "iris"))

# Still cannot write to non-default schema Throws an error.
dbWriteTable(con, Id(schema = "nycDoe", name = "iris"), iris)

可重现的例子

library(odbc)
library(dbplyr)
library(dplyr)
library(DBI)

con <- dbConnect(odbc(),
Driver = "SQL SERVER",
Server = "SERVER_NAME",
Database = "DB_NAME",
UID = "USER_NAME",
PWD = "PASSWORD")

# this works, writing to default schema
dbWriteTable(con, SQL("newTbleIris"), iris)

# this does NOT work and gives error mentioned above
dbWriteTable(con, SQL("EXISTING_SCHEMA.newTbleIris"), iris)

session 信息

- Session info --------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.5.2 (2018-12-20)
os Windows Server >= 2012 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate English_United States.1252
ctype English_United States.1252
tz America/New_York
date 2019-03-07

- Packages ------------------------------------------------------------------------------------------------------------------------
package * version date lib source
assertthat 0.2.0 2017-04-11 [1] CRAN (R 3.5.2)
backports 1.1.3 2018-12-14 [1] CRAN (R 3.5.2)
bit 1.1-14 2018-05-29 [1] CRAN (R 3.5.2)
bit64 0.9-7 2017-05-08 [1] CRAN (R 3.5.2)
blob 1.1.1 2018-03-25 [1] CRAN (R 3.5.2)
callr 3.1.1 2018-12-21 [1] CRAN (R 3.5.2)
cli 1.0.1 2018-09-25 [1] CRAN (R 3.5.2)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.5.2)
DBI * 1.0.0 2018-05-02 [1] CRAN (R 3.5.2)
dbplyr * 1.3.0 2019-01-09 [1] CRAN (R 3.5.2)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.5.2)
devtools 2.0.1 2018-10-26 [1] CRAN (R 3.5.2)
digest 0.6.18 2018-10-10 [1] CRAN (R 3.5.2)
dplyr * 0.8.0.1 2019-02-15 [1] CRAN (R 3.5.2)
fs 1.2.6 2018-08-23 [1] CRAN (R 3.5.2)
glue 1.3.0 2018-07-17 [1] CRAN (R 3.5.2)
hms 0.4.2 2018-03-10 [1] CRAN (R 3.5.2)
htmltools 0.3.6 2017-04-28 [1] CRAN (R 3.5.2)
httpuv 1.4.5.1 2018-12-18 [1] CRAN (R 3.5.2)
jsonlite 1.6 2018-12-07 [1] CRAN (R 3.5.2)
later 0.8.0 2019-02-11 [1] CRAN (R 3.5.2)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.5.2)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.5.2)
mime 0.6 2018-10-05 [1] CRAN (R 3.5.2)
odbc * 1.1.6 2018-06-09 [1] CRAN (R 3.5.2)
pillar 1.3.1 2018-12-15 [1] CRAN (R 3.5.2)
pkgbuild 1.0.2 2018-10-16 [1] CRAN (R 3.5.2)
pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.5.2)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.5.2)
prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.5.2)
processx 3.2.1 2018-12-05 [1] CRAN (R 3.5.2)
promises 1.0.1 2018-04-13 [1] CRAN (R 3.5.2)
ps 1.3.0 2018-12-21 [1] CRAN (R 3.5.2)
purrr * 0.3.0 2019-01-27 [1] CRAN (R 3.5.2)
R6 2.4.0 2019-02-14 [1] CRAN (R 3.5.2)
Rcpp 1.0.0 2018-11-07 [1] CRAN (R 3.5.2)
remotes 2.0.2 2018-10-30 [1] CRAN (R 3.5.2)
rlang 0.3.1 2019-01-08 [1] CRAN (R 3.5.2)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.5.2)
rstudioapi 0.9.0 2019-01-09 [1] CRAN (R 3.5.2)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.5.2)
shiny * 1.2.0 2018-11-02 [1] CRAN (R 3.5.2)
tibble 2.0.1 2019-01-12 [1] CRAN (R 3.5.2)
tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.5.2)
usethis 1.4.0 2018-08-14 [1] CRAN (R 3.5.2)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.5.2)
xtable 1.8-3 2018-08-29 [1] CRAN (R 3.5.2)

最佳答案

我找到了一个简单的解决方法。只需执行以下步骤:

首先,使用默认架构编写表:dbWriteTable(con, "newTbleIris", 虹膜)

然后将其插入到具有非默认模式的新模式中,但这次使用 dbGetQuery 函数:

dbGetQuery(con, "SELECT * INTO [schema].newTbleIris FROM newTbleIris")

就是这样!您的表现在将显示为非默认架构。

现在您可以删除您第一次使用默认架构创建的第一个表,方法是dbGetQuery(con, "DROP TABLE newTbleIris")

关于r - 尝试将表写入非默认模式时 SQL 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55106088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com