This post states the steps to connect R with SQL Server, so that we can directly access tables and can do analysis on data stored in SQL Server.
System Related Settings
1. Go to Control panel of your system.
2. Click on Administrative tools
![Connecting R with SQL Server1](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server1.jpg.pagespeed.ic.kF79lQK_1V.jpg)
3. Select User dsn -> click on “add” -> “Sql server”
![Connecting R with SQL Server2](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server2.jpg.pagespeed.ic.D07frUJBAw.jpg)
4. Give the details as:
![Connecting R with SQL Server3](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server3.jpg.pagespeed.ic.JTvkbKbUiR.jpg)
5. Click on Next :
![Connecting R with SQL Server4](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server4.jpg.pagespeed.ic.fLVfhMMfTr.jpg)
![Connecting R with SQL Server5](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server5.jpg.pagespeed.ic.OlBeMeKtE0.jpg)
Open R studio
1. Install and load RODBC package:
![Connecting R with SQL Server6](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server6.jpg.pagespeed.ic.2uxE--trxc.jpg)
![Connecting R with SQL Server7](https://web.archive.org/web/20170929171333im_/https://analyticskhoj.com/wp-content/uploads/2016/06/xConnecting-R-with-SQL-Server7.jpg.pagespeed.ic.as44I0ib2r.jpg)
2. Execute below Codes to connect R with SQL Server:
require(“RODBC”)
myconn <-odbcConnect(“Roma”, uid=”*******”, pwd=”*******”) pundat <- sqlQuery(myconn, “select top 2 * from Table_Trans_Currentweek”)
3. While exit, close the connection as:
close(myconn)