SQL Server's documentation indicates that the user name and password fields are UID
and PWD
. When I run this on my system, I also find that Driver = SQL Server
is problematic, but Driver=SQL Server
(no spaces around parentheses) works well. Try the following:
library(RODBC)
connStr <- paste("Server= 1112.1024.101.174",
"Database=stab",
"UID=tab",
"PWD=%12345",
"Driver=SQL Server",
sep =";"
)
channel <- odbcDriverConnect(connStr)
Optional: Using DBI
and odbc
You could also do something equivalent with the DBI
and odbc
packages. The advantage here is that you may form connections with dplyr
and not be required to work entirely in SQL if you don't want to (which has occasional advantages).
library(DBI)
library(odbc)
conn <- dbConnect(odbc(),
Driver = "SQL Server",
Server = "1112.1024.101.174",
Database = "stab",
UID = "tab",
PWD = "%12345")
Some other comments
If your Windows User name is stored in the SQL database, you can omit having to type in your password to make the connection by using the code below. This relies on Windows Authentication and we find it much preferable to having to enter passwords.
connStr <- paste("Server= 1112.1024.101.174",
"Database=stab",
"Trusted_Connection = yes",
"Driver=SQL Server",
sep =";"
)
If Windows Authentication isn't an option and you must add the password, I would strongly recommend upgrading to ODBC Driver 13 for SQL Server. The updated driver will force encryption of your user name and password when connecting to the server. The call would look like
connStr <- paste("Server= 1112.1024.101.174",
"Database=stab",
"Trusted_Connection = yes",
"TrustServerCertificate=yes", # See note below
"Driver=ODBC Driver 13 for SQL Server",
sep =";"
)
Note: TrustServerCertificate=yes
may be acceptable if you are on a protected network and are confident in the security. Otherwise, it may be necessary to install SSL certificates on your machine and the server.