Skip to content

Instantly share code, notes, and snippets.

@tgh0831
Last active September 2, 2020 19:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgh0831/6379253fd6d82671005127a843456aaa to your computer and use it in GitHub Desktop.
Save tgh0831/6379253fd6d82671005127a843456aaa to your computer and use it in GitHub Desktop.
This is a simple example using the RODBC package to return a query from a Microsoft SQL server to a data frame.
##### This is a simple RODBC example
##### The ODBCDriverName will be the driver name in ODBC Administrator
require(RODBC)
#open the ODBC connection
ch <- odbcConnect("ODBCDriverName")
##### Alternative ODBC connection for Microsoft SQL Server
ch <- odbcDriverConnect(
"Driver=SQL Server; Server=servername\\instance; Database=databasename; UID=username; Pwd=password"
)
#run the query, store in a data frame
sqlResult <- sqlQuery(ch, "SELECT ...
FROM ...
WHERE ...
;")
#close the ODBC connection
odbcClose(ch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment