Skip to content

copy_to fails to overwrite a temporary table #258

@mkirzon

Description

@mkirzon

Using SQL Server connections, overwriting a table created by copy_to() with temporary=TRUE does not work. As a workaround, we first have to run try(db_drop_table(con, "##test")) between calls to copy_to to ensure we can overwrite the temp table.

library(tidyverse)

# Initialize 2 dataframes
df1 = data_frame(value = c(1,2,3))
df2 = data_frame(value = c(4,5,6))

# Initialize connection and upload the first dataframe
con = DBI::dbConnect(odbc::odbc(), Driver = "SQL Server", Server = "SQLServerName", Database = "DBName")
tbl_tmp = copy_to(con, df1, name = "test", temporary = TRUE)
#> Created a temporary table named: ##test

# Test the first upload
tbl_tmp
#> # Source:   table<##test> [?? x 1]
#> # Database: Microsoft SQL Server
#> #   13.00.5216[user@SQLServerName/DBName]
#>   value
#>   <dbl>
#> 1     1
#> 2     2
#> 3     3

# Overwrite original table with the new dataframe
tbl_tmp = copy_to(con, df2, name = "test", temporary = TRUE, overwrite = TRUE)
#> Created a temporary table named: ##test
#> Error: <SQL> 'CREATE TABLE "##test" (
#>   "value" FLOAT
#> )
#> '
#>   nanodbc/nanodbc.cpp:1587: 42S01: [Microsoft][ODBC SQL Server Driver][SQL Server]There is already an object named '##test' in the database.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviordplyr verbs 🤖Translation of dplyr verbs to SQL

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions