Skip to content

SQL Server dbExists temporary table fix #724

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

Merged
merged 7 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# odbc (development version)

* SQL Server Fix `dbExists` for temporary table (@meztez, #724).

# odbc 1.4.1

* New `odbcListConfig()` lists configuration files on Mac and Linux
Expand Down
11 changes: 3 additions & 8 deletions R/driver-sql-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,12 @@ setMethod("dbExistsTable", c("Microsoft SQL Server", "character"),
function(conn, name, ...) {
stopifnot(length(name) == 1)
if (isTempTable(conn, name, ...)) {
name <- paste0(name, "\\_\\_\\_%")
df <- odbcConnectionTables(
conn,
name,
catalog_name = "tempdb",
schema_name = "dbo"
)
query <- paste0("SELECT OBJECT_ID('tempdb..", name, "')")
!is.na(dbGetQuery(conn, query)[[1]])
} else {
df <- odbcConnectionTables(conn, name = name, ...)
NROW(df) > 0
}
NROW(df) > 0
}
)

Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-driver-sql-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ test_that("SQLServer", {

test_that("dbExistsTable accounts for local temp tables", {
con <- DBItest:::connect(DBItest:::get_default_context())
con2 <- DBItest:::connect(DBItest:::get_default_context())
tbl_name <- "#myTemp"
tbl_name2 <- "##myTemp"
tbl_name3 <- "#myTemp2"
Expand All @@ -293,6 +294,8 @@ test_that("SQLServer", {
expect_true(!dbExistsTable(con, tbl_name2, catalog_name = "tempdb"))
# Fail because table not actually present
expect_true(!dbExistsTable(con, tbl_name3, catalog_name = "tempdb"))
# Fail because table was created in another live session
expect_true(!dbExistsTable(con2, tbl_name))
})

test_that("Create / write to temp table", {
Expand Down