-
Notifications
You must be signed in to change notification settings - Fork 109
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 to write to non-default schema
#91
Comments
dbWriteTable
fails to find tables in non-default schemas, lacks functionalitydbWriteTable
fails to find tables in non-default schemas
You need to use |
Thanks for the reply. After trying What I am finding out now (I think my original post did not correctly identify the error): writing to a non-default schema(.guest) produces the table dbo.guest.MikeTestTable without having created that table to begin with. What am I doing wrong? |
I also tried this:
This returns the error:
|
dbWriteTable
fails to find tables in non-default schemasdbWriteTable
fails to write to non-default schema
+1 - I am also trying to write to a nondefault schema and running into the same problem. |
@EthanTaft Hi, maybe you should try dropping the table before recreating it. |
I have the same problem. If the table does not exist using SQL("Schema.TableName") works fine. However, I am trying to append to the same table which creates the error described by EthanTaft. |
@aschmu Not too sure I follow. In my post it's not implied that it's "recreated" anywhere. I've made sure that I create it once in SSMS, then I try to write to it...I think you're trying to answer the "second" error. |
The SQLTable branch should make this work more smoothly. It will likely be merged with the master in the near future once we settle on the API. For now you can try it out using the following devtools::install_github("rstats-db/odbc@SQLTable")
library(DBI)
library(odbc)
con <- dbConnect(odbc::odbc(), "PostgreSQL")
# table named `mtcars`, schema named `abc`
tbl <- dbId(con, "mtcars", "abc")
tbl
#> <SQL> "abc"."mtcars"
dbWriteTable(con, tbl, mtcars)
dbExistsTable(con, tbl)
#> [1] TRUE
nrow(dbReadTable(con, tbl))
#> [1] 32
dbWriteTable(con, tbl, mtcars, append = T)
nrow(dbReadTable(con, tbl, row.names = FALSE))
#> [1] 64
dbWriteTable(con, tbl, mtcars, overwrite = T)
nrow(dbReadTable(con, tbl))
#> [1] 32
dbRemoveTable(con, tbl)
dbExistsTable(con, tbl)
#> [1] FALSE |
@jimhester Thanks for your work and response. I've downloaded the SQLTable branch and I am now getting an error when trying to build a connection string. Did something change in the branch for creating connections?
|
No, you may have to restart the R process before trying to load the new version of the package. |
Tried restarting and that didn't work. I re-installed from master and I get the same error so I uninstalled and re-installed from CRAN and now connection works but using CRAN version still getting original write issues. |
I used the dbId function and it works great for me. Thank you for the update. BTW i am using MS SQL Server on Ubuntu server and I connected to the database using the .connection_string parameter. |
@vicm159 What is the dbld function? I am using MS SQL in windows. |
It is the function defined in the branch I mentioned, I renamed it from |
@jimhester I got it working now using |
@jimhester I have run into a little bit of a problem. I have a couple of tables to append however every once in while i get this message Error in result_insert_dataframe(rs@ptr, values) : I'm not sure why since all these tables have the same type of data and only certain tables give me this message. Also, it looks like the message is truncated. Any way to see to the whole message? any idea why i keep getting this problem with? Thanks again for your help I really appreciate it. |
I don't know and can't really tell without being able to see the whole error message. There is no code in R , this package or it's dependencies that truncates the message, so the truncation must be happening in the driver or driver manager. I would try and enabling logging to see if you can get an actionable error message that way. See #96 for another issue with truncated error messages from SQL Server. |
Error in result_insert_dataframe(rs@ptr, values) : I had the same error and fixed it by changing the field catalog to an empty string in the object returned by dbId before passing it to dbWriteTable like so: tbl@catalog <- "" |
Yes, I forgot to mention i found the answer to this. I was uploading multiple tables (mainly with numeric columns) and most numeric columns did not have commas (example 5,000) but every once in a while one of the tables i was appending had commas. I'm guessing that dbWriteTable tried to append a character column with a numeric column in SQL Server and it threw that error. example the first table had column price = 1000 and the second table that needed to be appended to the first table had column price = 90,000 . Note to self make sure properly configure and class columns. |
I'm having the same issues as above. Namely, an attempt to write to a table in a non-default schema on SQL Server when using DBI::SQL("schema.table") results in R attempting to send a CREATE TABLE statement for a table that already exists. When I use the SQLTable branch to attempt to write to dev.MyTestTable per the example above: system.time({ Error in connection_sql_tables(conn@ptr, catalog_name = if (length(name@catalog) > : Setting the tbl catalog to blank does nothing to solve the problem. I love the speed for SELECT statements and have had no trouble reading data from my test table. Writing data back to anything other than the dbo schema fails consistently. |
Cross-posting from duplicate issue. I think one of the issues encountered above is that Example below to add color: So extending what I said, I think what you have uncovered @etiennebr is an inconsistency in the way that SQL interpretation happens wrt schemas. Again, something that will hopefully be taken into account and tested for as DBI adds schema support. Also, I reiterate my suggestion to do the following until that is the case.
Quick semantics - Check out this sequence: library(odbc)
library(DBI)
con <- dbConnect(odbc::odbc(), ...)
# I got this problematic sequence from the original example above
dbWriteTable(con, DBI::SQL("a"), data.frame(a = 1, b = 2))
dbWriteTable(con, DBI::SQL("public.a"), data.frame(a = 1, b = 2))
#> Error: <SQL> 'CREATE TABLE public.a (
#> "a" DOUBLE PRECISION,
#> "b" DOUBLE PRECISION
#> )
#> '
#> nanodbc/nanodbc.cpp:1587: 42P07: [RStudio][PostgreSQL] (30) Error occurred while trying to execute a query: [SQLState 42P07] ERROR: relation "a" already exists
#>
dbWriteTable(con, DBI::SQL("public.a"), data.frame(a = 1, b = 2), overwrite = TRUE)
#> Error: <SQL> 'CREATE TABLE public.a (
#> "a" DOUBLE PRECISION,
#> "b" DOUBLE PRECISION
#> )
#> '
#> nanodbc/nanodbc.cpp:1587: 42P07: [RStudio][PostgreSQL] (30) Error occurred while trying to execute a query: [SQLState 42P07] ERROR: relation "a" already exists
#>
# this is why that happens
dbExistsTable(con, DBI::SQL("public.a"))
#> [1] FALSE
dbWriteTable(con, "public.a", data.frame(a = 1, b = 2))
dbExistsTable(con, DBI::SQL("public.a"))
#> [1] TRUE
Essentially, when you say |
Digging into this in the course of another project - it seems that the underlying issue is Looking forward to enhanced schema behavior in DBI! |
I have the same problem in Postgresql. But switching the driver from RPostgres::Postgres() to RPostgreSQL::PostgreSQL() in connection solve it. con <- DBI::dbConnect(drv = RPostgreSQL::PostgreSQL(), DBI::dbWriteTable(con, |
It looks like
|
@LearningR1 that error indicates you need to update to the devel version of DBI |
I looked at my DBI version is 0.8, and there is another one called DBI test which is 1.5-2. |
You can use |
@jimhester, I use the command devtools::install_github("r-dbi/DBI" and library(DBI) , they are all successfull. I try to append data to the exsiting table. |
it only works when with creating a table |
@LearningR1 : I read and tried all the solutions from above, and same issue as you, and found a solution! In SQL Server Studio, do this:
Then do the usual Note to use actual table name (without schema prefix) in I'm using
|
@leungi Thanks for the work-around! That doesn't really fix the issue though. What if you don't have permission to alter a user or want to write to different schemas? |
for sql server, the following works for me, assuming the schema exists
|
for sql server I get a very disappointing mixed bag: table_id = DBI::Id(schema = 'schema_name', table = 'table_name')
dbWriteTable(con, table_id, data)
dbReadTable(con,table_id) both work. But dbExistsTable(con, table_id)
# as well as any custom query like
dbGetQuery(con, "select * from table_id") all fail. I was looking to replace RODBC but this is not workable currently for non-null schemas. |
@andrew57jm Just to be clear on your second failure (
|
Schema.table was what I tried first because that’s how it’s done in RODBC. It doesn’t work in odbc with a non-default schema. Using table_id was flailing. Everything works with default schema, unfortunately that’s not how my tables are organized.
…Sent from my iPhone
On Sep 1, 2018, at 3:50 PM, Cole Arendt ***@***.***> wrote:
@andrew57jm Just to be clear on your second failure (dbGetQuery), you are passing verbatim SQL to the database (explicit SQL). Nothing is accessible from the R context, so you should not expect the query to succeed unless table_id exists within the database. For dbGetQuery, you would need to use something like glue or paste to build a query string, or write the SQL verbatim:
dbGetQuery(con, "select * from schema_name.table_name")
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@andrew57jm It looks like you may have found some progress in #191 . In the future, a It also might be worth checking out #197 , where users are having some difficulty with similar behavior. |
Currently using DBI version 1.0.0 and odbc version 1.1.6 This currently gives an error;
However if I change the sql name to the following it works:
I'm not able to investigate as to why this is but I'd find it strange if this is the intended behaviour |
I still have the problem:
How can I help to debug this?
|
I am also facing problem with dbWriteTable with oracle database. I couldn't append existing table in the same schema as well as other schema. Below workaround also didn't work. |
Hi @GaneshHegde25 I'm having exactly the same problem with an Oracle Database. Did you solve the problem or found a workarround for this?. I've been trying for a week now to append obsertations and haven't had any advancements on this. Thank you in advance! |
I tried a few of the solutions above to no avail, below a workaround for others, which worked on a PostGreSQL database, RPostgres library. It sends an SQL command to my preferred schema search_path within a transaction block. I'm sure Oracle would have similar native SQL to accomplish the same: dbExecute(conn,"start transaction;") |
Workaround what I found is, creating temporary table and from there appending the actual table with SQL insert command. |
I'm also having this problem on SQL Server. |
I am getting the same issue even when I try to create a new table. |
I just updated library(dplyr)
library(dbplyr)
library(DBI)
library(MASS)
con <- dbConnect(odbc::odbc(), "LocalSQLExpress")
df <- as_tibble(Boston)
dbWriteTable(con, Id(schema = "IDL", table = "HousePrices"), df, overwrite = TRUE) |
I can also also confirm that the new CRAN version 1.2.1 (released Dec. 05, 2019) is fixing the issue finally on Microsoft SQL Server 2016 (and it seems to be unrelated to the used ODBC driver - I have tested it with the drivers Thanks Jim :-) |
@jimhester I just found this thread and I think this addresses the issue I am seeing. However, after attempting to ude Id(), I am still not able to get the dbWriteTable() function to work correctly. It seems like this issue has not yet been resolved for Oracle databases... I am trying the following: dbWriteTable(conn = asss_re_connect,
|
@pinkumbrella14: is Assuming it refers to con <- DBI::dbConnect(odbc::odbc(), dsn = <my_database_IP>, database = "ASSS", port = <my_port>) And when connecting to table, the DBI::dbWriteTable(conn = con, name = "ANN_NOL_FORECAST_TAB", value = data_table_rbindlist_, ...) |
Hi guys. I would really appreciate if you could help me with the following issue: I've already tried what you suggest but there is one thing that stops me from success. On my Oracle data warehouse I also have tablespaces. My connection to the database is OK but dbWriteTable see only datatables in the default tablespace (it's not a schema). We have two tablespaces - USERS1 (default) and USERS2. I want to append data to the table on the non-default tablespace USERS2. Do you think it requires some changes in connection step or am I right and it's just dbWriteTable case? My connection doesn't point any tablespace, just the database (and it works). |
Im my case, use: |
On which DB system and using which |
Issue Description and Expected Result
-
dbWriteTable
does not write to non-default schema. It instead writes to dbo.non-defaultschema.tablename
-Would like functionality to specify what schema within
dbWriteTable()
Database
SQL Server 2012
Reproducible Example
Upon further inspection, SSMS shows a table that exists with the schema and name of:
dbo.guest.MikeTestTable
The text was updated successfully, but these errors were encountered: