Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute SQL Server Stored Procedure in R

I have a problem when I execute a SQL Server 2008 stored procedure in R. The execution in R follows as:


library(RODBC)

dbhandle <- odbcDriverConnect('driver={SQLServer};server=xxx;database=xxx;trusted_connection=true') 

data <-sqlquery(dbhandle, "EXEC ihshsf35.FPR_GET_PF10  @tipo_pes='F' , @date_process='2016-11-30'") 

odbcCloseAll() 

The error that appears in R:

Error in as.POSIXlt.character(x, tz, ...) : 
 character string is not in a standard unambiguous format

The stored procedure calls a characteristic and a date to extracts information from the database:

Procedure [ihshsf35].[FPR_GET_PF10] 
( 
        @tipo_pes varchar(1), 
        @date_process as datetime 
) 
AS 
BEGIN 

I don´t have problems executing this stored procedure from other systems like SAS and SPSS, but when I try in R brings me a error. Note: In SQL server the data has the format "aaaa-mm-dd" the same I asking in R

Can anyone can help me with the problem in R?

Thanks in advance

like image 342
jareva Avatar asked Jan 07 '17 14:01

jareva


People also ask

How do I execute a stored procedure in R?

Run using RCall getInputParameters to get a list of input parameter objects. Define a $query or set a $value for each input parameter. Use executeStoredProcedure to execute the stored procedure from the R development environment, passing the list of input parameter objects that you set.

Can you run SQL code in R?

Did you know that you can run SQL code in an R Notebook code chunk? To use SQL, open an R Notebook in the RStudio IDE under the File > New File menu. Start a new code chunk with {sql} , and specify your connection with the connection=con code chunk option.

How do I execute a stored procedure in SQL Server?

In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. 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.

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.


2 Answers

Finally, I see what the problem was and how to solve it. Need to use the parameter as.is=T with the function sqlExecute:

data <- sqlExecute(dbhandle, "exec FPR_GET_PJ  @tipo_pes='J', @date_process='2016-12-31'", 
                   fetch = TRUE, as.is=T)
like image 144
jareva Avatar answered Sep 30 '22 03:09

jareva


Would you be wiling to give the following a try?

@date_process is expecting a date-time value, but I don't expect that would make much difference. I chose to use RODBCext because 1) safer from SQL Injection, and 2) Using single quotes (') has led me into more headaches with SQL than I care to recount.

library(RODBCext)

dbhandle <- odbcDriverConnect('driver={SQLServer};server=xxx;database=xxx;trusted_connection=true') 

data <-sqlExecute(dbhandle, 
                  "EXEC ihshsf35.FPR_GET_PF10  @tipo_pes=? , @date_process=?",
                  data = list(tipo_pes = 'F',
                              date_process = '2016-11-30 00:00:00'),
                  fetch = TRUE) 

odbcCloseAll() 

On the single quote issue, it's also worth trying a reversal of your single and double quotes. So 'EXEC ihshsf35.FPR_GET_PF10 @tipo_pes = "F", @date_process = "2016-11-30"'.

like image 36
Benjamin Avatar answered Sep 30 '22 03:09

Benjamin