SQL Server RODBC Connection
Asked Answered
V

5

62

Does anyone have a connection string example for using RODBC and connecting to MS SQL Server 2005 or 2008.

Thank you.

Vudimir answered 19/4, 2011 at 17:51 Comment(1)
Most connection string questions can be answered at connectionstrings.comSibley
W
108
library(RODBC)
dbhandle <- odbcDriverConnect('driver={SQL Server};server=mysqlhost;database=mydbname;trusted_connection=true')
res <- sqlQuery(dbhandle, 'select * from information_schema.tables')
Wageworker answered 19/4, 2011 at 19:46 Comment(6)
I'm trying this but am getting error "SQL Server does not exist or access denied." Do you have to enter a username/password? Also I have a dsn on my remote site but this does not seem to be mentioned. The RODBC docs were not all that clear to meGassy
The example uses the option "trusted_connection=true" for automatic windows authentication. You can also provide username=... and password=... as parameters in the connection string.Wageworker
actually for me username and password did not work - i had to provide the parameters uid=...; and pwd=...;.Poultry
Any ideas how to obtain column names from the DW into R?Ship
NOTE I had to use the IPV6 Ip address instead of the server name to get this to work.Divorce
Just for what it's worth, if you are familiar with the DBI interface, the newer odbc package is DBI-compliant and an alternative to the RODBC package.Shanna
P
8

Taken from a posting to r-help:

 library(RODBC)
 channel <- odbcDriverConnect("driver=SQL Server;server=01wh155073")
 initdata<- sqlQuery(channel,paste("select * from  test_DB .. 
 test_vikrant"))
 dim(initdata)
 odbcClose(channel)
Pointtopoint answered 19/4, 2011 at 19:48 Comment(2)
How would you do this from Mac OSX? Same way? Is that possible, with the windows login requirement?Survey
Seems unlikely this would work using same machine with MS's SQL Server on a Mac, but with some sort of remote access terminal arrangement, perhaps.Pointtopoint
Z
5

If you have to include the USERNAME and PASSWORD:

library(RODBC) # don't forget to install it beforehand

my_server="ABC05"
my_db="myDatabaseName"
my_username="JohnDoe"
my_pwd="mVwpR55zobUldrdtXqeHez"


db <- odbcDriverConnect(paste0("DRIVER={SQL Server};
                                 server=",my_server,";
                                 database=",my_db,";
                                 uid=",my_username,";
                                 pwd=",my_pwd))


sql="SELECT * FROM dbo.MyTableName" #dbo is the schema here
df <- sqlQuery(db,sql)
Zenobiazeolite answered 30/1, 2019 at 23:41 Comment(0)
G
1

Try to use RSQLS package: https://github.com/martinkabe/RSQLS

Very fast pushes data from data.frame to SQL Server or pulls from SQL Server to data.frame.

Example:

library(devtools)
install_github("martinkabe/RSQLS")
library(RSQLS)

cs <- set_connString("LAPTOP-USER\\SQLEXPRESS", "Database_Name")
push_data(cs, dataFrame, "dbo.TableName", append = TRUE, showprogress = TRUE)
df <- pull_data(cs, "SELECT * FROM dbo.TableName", showprogress = TRUE)

This solution is much faster and more robust than RODBC::sqlSave or DBI::dbWriteTable.

Getz answered 9/2, 2018 at 18:24 Comment(0)
P
0

First You have to Create/configure DSN (ODBC connection with specific DB)

Then install RODBC library.

library(RODBC)
myconn <-odbcConnect("MyDSN", uid="***", pwd="*******")

fetchData<- sqlQuery(myconn, "select * from tableName")
View(fetchData)
close(myconn)
Pinochle answered 14/6, 2017 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.