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.
Using SQL Server connections, overwriting a table created by
copy_to()withtemporary=TRUEdoes not work. As a workaround, we first have to runtry(db_drop_table(con, "##test"))between calls tocopy_toto ensure we can overwrite the temp table.