Connect to SQL Server in R without ODBC DNS connection
In the previous post, I have explained the steps to connect to SQL Server with ODBC DSN.
In this blog post, I am going to connect to SQL Server without ODBC DSN. But, will be using driver name instead.
Step 1: RODBC must be installed in R. Please read (Connect to SQL Server in R for detail)
Step 2: Load the RODBC library
Step 3:Connecting to SQL Server with Driver name
MSConnection <- odbcDriverConnect(“DRIVER=SQL SERVER;SERVER=.;Trusted_Connection=Yes”)
This connection point to the default database initially. But, we can run USE statement to change the database.
sqlQuery(MSConnection, “USE test”)
Step 4: Read the table data to a variable
mydata <- sqlQuery(MSConnection, “Select * from dates”)
Step 5: Display data by just typing the variable name in R command environment
Step 6: Close the connection