RODBC : Single Backslash in server name [closed]

2019-08-16 04:27发布

问题:

I have the following issue : the SQL server I am trying to connect to using RODBC has a backslash in it.

Here is my code:

library(RODBC)
server <- "servername\REP"
database<- "databasename"
connectionString <- paste("Driver={SQL Server};server=",server,";database=",database,";trusted_connection=yes;")
channel <-  odbcDriverConnect(connection=connectionString)

Of course \R causes an issue and the channel cannot be opened. One solution would be to escape the backslash:

   server <- "servername\\REP"

But then the server name is not the right one anymore and the connection cannot be made.

Basically I am stuck needing server <- "servername\REP" to work!

Surely someone has already encountered this issue...

回答1:

Ok so the solution here was using "paste0" to generate the connection string :

library(RODBC)
server <- "servername\REP"
database<- "databasename"
connectionString <- paste0("Driver={SQL Server};server=",server,";database=",database,";trusted_connection=yes;")
channel <-  odbcDriverConnect(connection=connectionString)

using "paste()" was adding pesky white spaces.