-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dbWriteTable fails for non-default schemas #197
Comments
The odbc writes fine to a default schema, but haven't been able to write to a non-default schema. Any help would be most appreciated. Thanks. |
You need to use |
Thanks for that Jim. Have tried this but same error is happening as below: library("DBI")
con <- DBI::dbConnect(odbc::odbc(),
Driver = "SQL Server",
Server = "ADS-UAT",
Database = "Sandbox",
Trusted_Connection = "True")
table_id <- Id(schema = "Belmont",
table = "IRIS")
res <- dbWriteTable(conn = con,
name = table_id,
value = iris,
append = TRUE)
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 |
Interesting. Is your connection in a good state immediately prior to the What version of SQL Server are you connecting to? And are you connecting from a Windows machine to get Trusted_Connection? What type of ODBC driver are you using? I am unable to repro from linux using standard connection. However, did get an interesting error trying to append. library("DBI")
cfg <- config::get(file = "~/wrk/conn.yml")
con <- do.call(DBI::dbConnect, cfg$mssql)
table_id <- Id(schema = "Belmont",
table = "IRIS")
res <- dbWriteTable(conn = con,
name = table_id,
value = iris,
append = TRUE,
overwrite = FALSE)
head(dbReadTable(con, table_id))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
res <- dbWriteTable(conn = con,
name = table_id,
value = iris,
append = TRUE,
overwrite = FALSE)
#> Error: <SQL> 'CREATE TABLE "Belmont"."IRIS" (
#> "Sepal.Length" FLOAT,
#> "Sepal.Width" FLOAT,
#> "Petal.Length" FLOAT,
#> "Petal.Width" FLOAT,
#> "Species" varchar(255)
#> )
#> '
#> nanodbc/nanodbc.cpp:1587: 42S01: There is already an object named 'IRIS' in the database., Created on 2018-07-21 by the reprex package (v0.2.0). |
Hi, I have similar problems. I can't get anything to work.
other attached packages: What can I do? |
I ended up just writing a server side process which moves the table from
the default schema to desired schema. I call the stored p procedure using
the rodbc package. The odbc passage moves the data quickly into the
database. The rodbc triggers a fast server side process to move and delete
the temporary default schema table.
|
Yuck! That should definitely not be necessary - have you seen issue #91 ? A handful of other solutions there. I'll try to take a look and see if I can repro today.
|
My SQL Version is 14.0.1000.169, I'm connecting from windows 10. I don't know what type odbc driver, I have. I'm using odbc package in R. Package version are in my comment above. |
For troubleshooting this, can you please try the following: odbc:::connection_sql_tables(
con@ptr,
catalog_name = "",
schema_name = "<your schema name>",
table_name = "<your table name>"
) The odbc package uses (This won't fix the problems with creating the table, but we might be able to better understand what's going on.) |
Ok, Here is what I have. I created by hand "Table1" to Schema "other" in "Test" Catalog.
So looks like it will find it, but If I leave catalog_name empty as in your example:
And "%" gives similar cursor state error, what I have in creating table.
|
Thanks. What about odbc:::connection_sql_tables(
con@ptr,
catalog_name = NULL,
schema_name = "<your schema name>",
table_name = "<your table name>"
) ? |
Sorry for late response, here is what I get.
SO seems to work with NULL |
I maybe had the same problem. I had the change the table name to inclucde a "_" for the database table. This seemed to be a condition by the database. |
Piping in to say I have the exact same situation as @hakki13. I am connecting to SQL Server 13.0.4223.10 from Windows 10 with odbc 1.1.6. I receive the same invalid cursor state error when trying to write to the non-default schema. Below is the result of the connection_sql_tables(). Note the potential extra wrinkle of the dot in the table_catalog.
|
Hoping to provide a bit more to help with debugging. # Cannot append to table in non-default schema - why?
library(DBI)
con <- dbConnect(odbc::odbc(), Driver = "SQL Server", server = "CL-SQL-APPDEV",
database = "CIA_1_0", port = 1433L)
table <- Id(catalog = "CIA_1_0", schema = "import", table = "test")
df <- data.frame(a = c(1, 3),
b = c(4, 5))
post <- dbWriteTable(con, name = table, value = df, append = TRUE)
#> Error: Can't unquote "CIA_1_0"."import"."test"
# It appears that something is not working with this round trip. Should this succeed?
q <- dbQuoteIdentifier(con, table)
u <- dbUnquoteIdentifier(con, q)
#> Error: Can't unquote "CIA_1_0"."import"."test"
DBI::dbDisconnect(con) Created on 2018-09-20 by the reprex package (v0.2.0). Session infodevtools::session_info()
#> Session info -------------------------------------------------------------
#> setting value
#> version R version 3.5.1 (2018-07-02)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> tz America/Los_Angeles
#> date 2018-09-20
#> Packages -----------------------------------------------------------------
#> package * version date source
#> backports 1.1.2 2017-12-13 CRAN (R 3.5.0)
#> base * 3.5.1 2018-07-02 local
#> bit 1.1-14 2018-05-29 CRAN (R 3.5.0)
#> bit64 0.9-7 2017-05-08 CRAN (R 3.5.0)
#> blob 1.1.1 2018-03-25 CRAN (R 3.5.1)
#> compiler 3.5.1 2018-07-02 local
#> datasets * 3.5.1 2018-07-02 local
#> DBI * 1.0.0.9000 2018-09-05 Github (r-dbi/DBI@dbc32a0)
#> devtools 1.13.6 2018-06-27 CRAN (R 3.5.1)
#> digest 0.6.15 2018-01-28 CRAN (R 3.5.1)
#> evaluate 0.10.1 2017-06-24 CRAN (R 3.5.1)
#> graphics * 3.5.1 2018-07-02 local
#> grDevices * 3.5.1 2018-07-02 local
#> hms 0.4.2 2018-03-10 CRAN (R 3.5.1)
#> htmltools 0.3.6 2017-04-28 CRAN (R 3.5.1)
#> knitr 1.20 2018-02-20 CRAN (R 3.5.1)
#> magrittr 1.5 2014-11-22 CRAN (R 3.5.1)
#> memoise 1.1.0 2017-04-21 CRAN (R 3.5.1)
#> methods * 3.5.1 2018-07-02 local
#> odbc 1.1.6 2018-06-09 CRAN (R 3.5.1)
#> pkgconfig 2.0.1 2017-03-21 CRAN (R 3.5.1)
#> Rcpp 0.12.18 2018-07-23 CRAN (R 3.5.1)
#> rlang 0.2.2.9000 2018-08-20 Github (r-lib/rlang@2cf70c8)
#> rmarkdown 1.10 2018-06-11 CRAN (R 3.5.1)
#> rprojroot 1.3-2 2018-01-03 CRAN (R 3.5.1)
#> stats * 3.5.1 2018-07-02 local
#> stringi 1.1.7 2018-03-12 CRAN (R 3.5.0)
#> stringr 1.3.1 2018-05-10 CRAN (R 3.5.1)
#> tools 3.5.1 2018-07-02 local
#> utils * 3.5.1 2018-07-02 local
#> withr 2.1.2 2018-03-15 CRAN (R 3.5.1)
#> yaml 2.1.19 2018-05-01 CRAN (R 3.5.1) |
Here's some more info. I'm hoping this will elicit a response from the experts. library(DBI)
# This works
id1 <- Id(schema = "my_schema", table = "my_table")
unquoted <- dbUnquoteIdentifier(ANSI(), id1)
# This works
id2 <- Id(catalog = "my_db", schema = "my_schema", table = "my_table")
unquoted2 <- dbUnquoteIdentifier(ANSI(), id2)
### Round-tripping
# This works
q1 <- dbQuoteIdentifier(ANSI(), id1)
rt1 <- dbUnquoteIdentifier(ANSI(), q1)
# This fails
q2 <- dbQuoteIdentifier(ANSI(), id2)
rt2 <- dbUnquoteIdentifier(ANSI(), q2)
#> Error: Can't unquote "my_db"."my_schema"."my_table" Created on 2018-09-25 by the reprex package (v0.2.0). The offending code appears to be these lines in quote.R:
If your Id contains a catalog, then the SQL object created by dbQuoteIdentifier cannot pass the regex test above. I'm not competent enough with regex to know how to fix it, but I'm hoping it's straightforward. |
I am experiencing the same issue described here. I am using: Microsoft SQL Server Version: 12.00.5207 And here is my session info:
Running:
Do you suggest any quick-fix? |
I have been having this issue as well. I can get it to work by using an underscore in my table name. I have no idea why it works, but it does.
|
Hi |
I tried the same code, appending new rows in the no-default schema of SQL Server Database. With the packages in the last version (CRAN) :
Without catalog, something goes wrong in the dbWriteTable code.
I installed the SQLTable branch from odbc and ran the same code.
|
Hi, Is there any chance that we see this issue resolved in odbc??
but I got this error: |
I reproduced the issue writing to schemas and appending, it ultimately came down to driver interpretations of the Hopefully it will not cause other issues in other databases. |
Im my case, use: |
Following up on issue 91 and
#175
, we haven't been able to get this working.
We're just trying to write to a non-default schema using the tableID's as suggested.
Code is:
The text was updated successfully, but these errors were encountered: