Firstly, I'm having an issue where I can't access databases on a particular MSSQL server if said databases contain a hyphen ("-") in the name, like DATA-WAREHOUSE. All the other databases on said MSSQL Server, the ones without hyphens, work just fine on the same connection.
Secondly, the Connections Pane in the RStudio doesn't show the contents of any of the databases that have hyphens in their names (for the MSSQL Server in question).
On the first issue, the error messages are varied as I've made many different attempts but simply writing Database = "DATA-WAREHOUSE" (where Database = "nohyphen" works) in the dbConnect function call gives up the following error (which, to be clear is not descriptive as accessing the hyphenated databases with the service principal in question is not an issue in SSMS or dBeaver)
Error in `dbConnect()`:
! ODBC failed with error 00000 from [Microsoft][ODBC Driver 18 for SQL Server][SQL Server].
✖ Cannot open database "DATA-WAREHOUSE" requested by the login. The login failed.
• Login failed for user 'myserveruser'.
• Invalid connection string attribute
ℹ From nanodbc/nanodbc.cpp:1184.
Run `rlang::last_trace()` to see where the error occurred.
Example connections to the same server, different databases
library(DBI)
library(odbc)
library(dbplyr)
library(tidyverse)
connection_success = dbConnect(odbc(), Driver = "drivername",
Server = "myserver,1234", Database = "THISWORKS",
TrustServerCertificate = "yes",
UID = Sys.getenv("myserveruser"),
PWD = Sys.getenv("myserverpassword"),
Port = 1234)
no_connection = dbConnect(odbc(), Driver = "drivername",
Server = "myserver,1234", Database = "THIS-DOESNT",
TrustServerCertificate = "yes",
UID = Sys.getenv("myserveruser"),
PWD = Sys.getenv("myserverpassword"),
Port = 1234)
still_no_dice = dbConnect(odbc(), Driver = "drivername",
Server = "myserver,1234", Database = "[THIS-DOESNT-EITHER]",
TrustServerCertificate = "yes",
UID = Sys.getenv("myserveruser"),
PWD = Sys.getenv("myserverpassword"),
Port = 1234)
just_nope = dbConnect(odbc(), Driver = "drivername",
Server = "myserver,1234", Database = DBI::SQL("CRYING-EMOJI"),
TrustServerCertificate = "yes",
UID = Sys.getenv("myserveruser"),
PWD = Sys.getenv("myserverpassword"),
Port = 1234)
Connecting using dbConnect() without specifying the Database name works, but then it just fails when I try to reference the database with in_catalog(), or I()
tbl(successful_con_object, I("[DATA-WAREHOUSE].myschema.my_table"))
Error in `db_query_fields.DBIConnection()`:
! Can't query fields.
ℹ Using SQL: SELECT * FROM [DATA-WAREHOUSE].myschema.my_table "q03" WHERE (0 = 1)
Caused by error in `tbl()`:
! ODBC failed with error 00000 from [Microsoft][ODBC Driver 18 for SQL Server][SQL Server].
✖ The server principal "myserveruser" is not able to access the database "DATA-WAREHOUSE" under the current security context.
• <SQL> 'SELECT *
• FROM [DATA-WAREHOUSE].myschema.my_table "q03"
• WHERE (0 = 1)'
ℹ From nanodbc/nanodbc.cpp:1802.
Run `rlang::last_trace()` to see where the error occurred.
Any and all help welcome!