odbc icon indicating copy to clipboard operation
odbc copied to clipboard

dbGetQuery no longer works with stored procedures

Open gitkatdev opened this issue 4 years ago • 0 comments

Issue Description and Expected Result

I've just install R version 4.0.3 (until last week I was using R 3.6) with DBI 1.1.1 and I'm facing a new error that I've never seen before. I have a MySQL database (version 8.0.18) and I use DBI to retrieve data. Specifically, the database includes some stored procedures that prepare a sql statement based on input parameters and execute it. Until last week I was able to retrieve the output of these stored procedures using dbGetQuery (as indicated in the DBI documentation) while after the upgrade to R 4.0.3 I get the following error:

Error in result_fetch(res@ptr, n) : nanodbc/nanodbc.cpp:2695: 24000: [MySQL][ODBC 8.0(w) Driver][mysqld-8.0.23]Fetch without a SELECT

Database

[MySQL][ODBC 8.0(w) Driver][mysqld-8.0.23]

Reproducible Example

Assume there is a simple table called "test_1" like this: intId | strName 1 | Rome 2 | New York 3 | Prague and a stored procedure # called "my_stored_proc" with two input parameters (table_name and id) and the following structure:

BEGIN: SET @t1 =CONCAT('SELECT strName FROM ', table_name, ' WHERE intId =', id, ';' ); PREPARE stmt3 FROM @t1; EXECUTE stmt3; DEALLOCATE PREPARE stmt3; END

In R I then run the following code:


library(odbc)
library(DBI)
strCnn <- paste0("Driver={MySQL ODBC 8.0 UNICODE Driver};
                              Server=***;
                              Database=***;
                              Port=***;
                              User=***;
                              Password=***;
                              trusted_connection=true;
                              ENABLE_LOCAL_INFILE=1;")
cnn <- dbConnect(odbc::odbc(), .connection_string = strCnn, bigint="numeric"

ref_tbl <- "test_1"
ref_id <- 1
out <- paste0("CALL my_stored_proc('", ref_tbl, "', ", ref_id, ");") %>% dbGetQuery(CNN_NOAH, .) %>% as.numeric()

In the paste the above code returns out == "Rome" while I now get the error mentioned above.

Is this an error on my side or a bug?

Thank you in advance Isacco

gitkatdev avatar Jan 29 '21 09:01 gitkatdev