Last active
May 4, 2021 16:11
-
-
Save tbobin/424fc7b45dd9a0324d3416a8ff689322 to your computer and use it in GitHub Desktop.
Execute stored procedures from R with odbc and DBI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
What about with a parameter for the stored procedure?