Skip to content

Instantly share code, notes, and snippets.

@tbobin
Last active May 4, 2021 16:11
Show Gist options
  • Save tbobin/424fc7b45dd9a0324d3416a8ff689322 to your computer and use it in GitHub Desktop.
Save tbobin/424fc7b45dd9a0324d3416a8ff689322 to your computer and use it in GitHub Desktop.
Execute stored procedures from R with odbc and DBI
library(tidyverse)
library(DBI)
library(odbc)
# connect to database
cn <- dbConnect(odbc::odbc(), driver = "ODBC Driver 13 for SQL Server",
server="MYSQLSERVER\\SQLEXPRESS",
database = "myTestDB",
Trusted_Connection="Yes")
# normal query with select statement
cn %>% dbGetQuery("select * from [dbo].Tbl;")
# get a stored procedure
cn %>% dbGetQuery("EXEC [dbo].[meineProzedur]")
cn %>% dbDisconnect()
@markblue777
Copy link

What about with a parameter for the stored procedure?

@tbobin
Copy link
Author

tbobin commented Feb 2, 2021

You can add the parameter to the Query string like:

foo <- cn %>% dbGetQuery("EXEC [dbo].[MyProcedure] @MyParam = 'bar'")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment