Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a SQL Server Stored Procedure with Parameter in R

Tags:

sql

sql-server

r

Can anyone give code to call a SQL server Stored Procedure with Parameter in R?

This is all I have and it is not working:

dbhandle <- odbcDriverConnect('driver={SQL Server};server=SWD-CHISSIS01;database=GlobalRiskManagement;trusted_connection=true')
data <-sqlQuery(dbhandle, "EXEC my_Stored_Proc  @jyear=2013, @ddate=9/25/2013")
odbcCloseAll()

Thank you.

like image 558
user2821029 Avatar asked Sep 26 '13 19:09

user2821029


People also ask

Which function can be used to call stored procedures from R?

Use executeStoredProcedure to execute a stored procedure defined as an StoredProcedure object. Call this function only when executing a stored procedure from R code.

How do you call a parameterized stored procedure?

Expand the database that you want, expand Programmability, and then expand Stored Procedures. Right-click the user-defined stored procedure that you want and select Execute Stored Procedure. In the Execute Procedure dialog box, specify a value for each parameter and whether it should pass a null value.

What is an an R stored procedure?

An R stored procedure is basically an R function that can be used inside SQL Server. In this tip we will use RStudio to create and test our R code and deploy it to SQL Server as a stored procedure. In order to create a stored procedure inside RStudio, we need to install and use the sqlutils R package.

How do I create a stored procedure with parameters in SQL?

Creating a SQL Stored Procedure with Parameters. To create a stored procedure with parameters using the following syntax: CREATE PROCEDURE dbo.uspGetAddress @City nvarchar(30) AS; See details and examples below; SQL Server Query to Turn into a Stored Procedure. Below is the query we want to use to create the stored procedure.

What is a stored procedure?

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it. You can also pass parameters to a stored procedure, so that the stored procedure can act based on ...

Can we use null as default parameter in SQL Server procedure?

SQL Server Stored Procedure using NULL as Default Parameter In most cases it is always a good practice to pass in all parameter values, but sometimes it is not possible. So in this example we use the NULL option to allow you to not pass in a parameter value.


2 Answers

Just to clarify, the in the code above, the **'s should not be included in the code so it should say: data <-sqlQuery(dbhandle, "set nocount on\nEXEC my_Stored_Proc @jyear=2013, @ddate=9/25/2013")

This works for me. Thanks to all who replied.

like image 63
David L. Avatar answered Oct 07 '22 19:10

David L.


dbhandle <- odbcDriverConnect('driver={SQL Server};server=SWD-CHISSIS01;database=GlobalRiskManagement;trusted_connection=true')
data <-sqlQuery(dbhandle, "**set nocount on\n**EXEC my_Stored_Proc  @jyear=2013, @ddate=9/25/2013")
odbcCloseAll()
like image 38
Bharathi Raja Avatar answered Oct 07 '22 20:10

Bharathi Raja