Connect to SQL Server RODBC

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

Thank.

+56
r rodbc
Apr 19 2018-11-17T00:
source share
5 answers
library(RODBC) dbhandle <- odbcDriverConnect('driver={SQL Server};server=mysqlhost;database=mydbname;trusted_connection=true') res <- sqlQuery(dbhandle, 'select * from information_schema.tables') 
+94
Apr 19 2018-11-11T00:
source share

Taken from the publication in 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) 
+8
April '11 at 19:48
source share

Try using the RSQLS package: https://github.com/martinkabe/RSQLS

It transfers data very quickly from data.frame to SQL Server or 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 reliable than RODBC :: sqlSave or DBI :: dbWriteTable .

+1
Feb 09 '18 at 18:24
source share

First you need to create / configure a DSN (ODBC connection to a specific database).

Then install the RODBC library.

 library(RODBC) myconn <-odbcConnect("MyDSN", uid="***", pwd="*******") fetchData<- sqlQuery(myconn, "select * from tableName") View(fetchData) close(myconn) 
0
Jun 14 '17 at 6:45
source share

If you need to specify a 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) 
0
Jan 30 '19 at 23:41
source share



All Articles