r-dbi / odbc

Connect to ODBC databases (using the DBI interface)
https://odbc.r-dbi.org/
Other
387 stars 107 forks source link

dbListFields() fails with non-default schema table with odbc + SQL Server #822

Closed erikking closed 2 months ago

erikking commented 2 months ago

Run following code

conn <- DBI::dbConnect(drv=odbc::odbc(), Driver="SQL Server", Server="xxx", Database="xxx", Trusted_Connection="yes")
DBI::dbListFields(conn, DBI::Id(schema="schema_name", table = "table_name"))

return error

Error: nanodbc/nanodbc.cpp:1752: 42000: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near '0'.  [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. 
<SQL> 
SELECT * FROM  "schema_name"."table_name" LIMIT 0

The LIMIT 0 is not correct syntax for SQL Server, should be TOP 0, looks like there is hard code in https://github.com/r-dbi/DBI/blob/905c0d1b238302de624ce742b06b10f528f2baac/R/03-DBIConnection.R#L34

Hope it can be resolved, thanks in advance.

detule commented 2 months ago

Hi @erikking

Can you provide some information about your environment ( os, driver, package:odbc and package:DBI version ).

With 1.5.0 from CRAN I can't replicate your issue - either that or I don't understand the problem.

> conn@info
...
$dbms.name
[1] "Microsoft SQL Server"
...
$drivername
[1] "libmsodbcsql-17.10.so.6.1"
...
> sessionInfo()
...
other attached packages:
[1] odbc_1.5.0
...
loaded via a namespace (and not attached):
...
[5] DBI_1.2.2

> dbGetQuery(conn, "SELECT DB_NAME()")
1 deleteme
> DBI::dbListFields(conn, DBI::Id(schema="testschema", table = "tbl"))
[1] "CustomerUUID"  "BranchNumber"  "ArticleNumber" "ValidOn"
[5] "Comment"
erikking commented 2 months ago

@detule

Thanks for your reply. Please see my environment

> conn@info
$dbms.name
[1] "Microsoft SQL Server"

$drivername
[1] "SQLSRV32.DLL"

$odbc.version
[1] "03.80.0000"

$driver.version
[1] "10.00.17763"

$odbcdriver.version
[1] "03.52"

attr(,"class")
[1] "Microsoft SQL Server" "driver_info"          "list"  
sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.936  LC_CTYPE=English_United States.936    LC_MONETARY=English_United States.936
[4] LC_NUMERIC=C                          LC_TIME=English_United States.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] odbc_1.3.4 DBI_1.2.3 
detule commented 2 months ago

Hi:

BLUF: Can you test with a recent version of package:odbc - I think the version you are using is ~ 2 years old.

I think part of the issue is that you are using a very up-to-date packge:DBI, and somewhat dated package:odbc. At some point, there was a change to the DBI::Id API ( and I think your recent version includes this change ). I think it necessitates using a relatively recent package:odbc that matches those changes.

Let me know what you find with an up-to-date package:odbc. If still reproducible, we can dive further, though to be perfectly honest SQLSRV32.DLL ( legacy MS Access ODBC adapter?) is not a driver we normally support in the issue section.

erikking commented 2 months ago

Got it. I updated odbc to latest version 1.5.0, then it works for either of driver SQLSRV32.DLL and msodbcsql18.dll.

Thanks very much.