library(RODBC) #host<-"rmsftdp.northeurope.cloudapp.azure.com" host<-"104.45.80.173" db<-"AWDW16" uname<-"Rdemo" pwd<-"tugait" connstring<-sprintf("DRIVER={ODBC Driver 11 for SQL Server};Server=%1$s;Database=%2$s;Uid=%3$s;Pwd=%4$s;",host,db,uname,pwd) conn<-odbcDriverConnect(connstring) # Test! if(conn!=-1L) knitr::kable(sqlTables(conn,schema = "dbo"))
RODBC comes with some built-in functions for working with your database
dt<- sqlFetch(conn, "DatabaseLog") sqlCopy( conn, "select * from DatabaseLog", "DatabaseLog2") sqlSave( conn, dt[1:100,], "DatabaseLog2") sqlClear(conn, "DatabaseLog2") sqlDrop( conn, "DatabaseLog2")
Use normal SQL statements using sqlQuery()
- must send one statement at a time to get the results back from each.
dt<- sqlQuery(conn," SELECT DatabaseLogID ,DatabaseUser ,Event ,[Schema] ,Object FROM DatabaseLog ") knitr::kable(head(dt))
str( sqlQuery(conn," SELECT COUNT(*) as Vol FROM DatabaseLog; SELECT DatabaseLogID FROM DatabaseLog; ") )
Microsoft R Server comes with an additional package RevoScaleR
that allows you to connect to SQL Server and process very large amounts of data without loading it all into memory. This package is not available in R or Microsoft R Server Open.
Read more about it Getting Started with RevoScaleR
sales<- sqlQuery(conn," SELECT TOP 10 * FROM FactResellerSales f INNER JOIN DimReseller r ON f.resellerkey = r.resellerkey ") knitr::kable(head(sales))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.