How To Connect MS SQL Server With R


Generally I process big amount of data in the database like Oracle, MS SQL Server, DB2 or MySQL. So, if I want to work with those data I have to download the data first then I have to upload those data. Sometimes, this kind of task may be huge time consuming and painful. So, I started to think how I can connect MS SQL Server with R. I searched many web pages but did not find any suitable solution. In the below code change only the bold marked parts and keep the other parts as it is.

#initialization odbc library
library("RODBC")
#connecting database with odbc driver
dbconnection <- odbcDriverConnect("Driver={SQL Server};Server=minhaj_srv; Database=minhaj_db;Uid=minhaj; Pwd=minhaj;Port=1413;trusted_connection=yes")
#this will create a data frame form the below query using the odbc driver object
tbld_product <- sqlQuery(dbconnection,paste("select * from [minhaj_db].[dbo].[tbld_product];"))
#View the result
View(tbld_product)
#now you can use the data frame according to your needs.

The above code is tested and hope you will not face any problem with the code. Very soon we will incorporate video with the writeup and practically you can see how to connect MS SQL Server with R. If you face any issue in the above code don’t hesitate to write your problem on our facebook pageĀ https://www.facebook.com/LearningBigDataAnalytics/

Add a Comment