将多个参数传递给 [sp_execute_external_script] 以执行 R 代码

Passing More then One Parameter to [sp_execute_external_script] for Executing R code

这里是我试图传递三个参数的场景,即 empid2Name1 & DOB1。我无法传递参数并在 OutPutDataset 处获取结果数据帧。 Observed:通过一个参数我得到了结果但是如果有一个以上的参数我无法得到输出数据帧。

ALTER procedure [dbo].[spGetAllEmployees] 
@empid2 int,
@Name1 Varchar(50)
--@DOB1 Date   
as    
Begin    
Declare @empid int
Declare @NewName varchar(50)

EXECUTE sp_execute_external_script
 @language = N'R'
,@script = N'df <- as.data.frame(InputDataSet);df[,1] <- df[,1]+1;OutputDataSet <-df'
,@input_data_1 = N'select * from tblEmployee where EmployeeId=@empid AND Name=@NewName'
,@params = N'@empid Int OUTPUT'
,@empid = @empid2
,@NewName =@Name1
--,@NewDOB=@DOB1
WITH RESULT SETS (([Col1] Int, [Col2] varchar(50), [Col3] varchar(50), Col4 varchar(50),Col5 varchar(50)));

End

-- EXEC dbo.spGetAllEmployees @empid2 ='1', @Name1='Nabi';  

使用上述参数 empid2Name1 & DOB1 来自给定数据 Frame/Data Table 我们正在尝试对 Table 进行子集化。

您需要在查询中写入 输出 语法 @Params

ALTER procedure [dbo].[spGetAllEmployees] 
        @empid2 int,
        @Name1 Varchar(50),
        @DOB1 Date   
    as    
    Begin    
    Declare @empid int
    Declare @NewName varchar(50)
    Declare @NewDOB Varchar(50)

        EXECUTE sp_execute_external_script
         @language = N'R'
        ,@script = N'df <- as.data.frame(InputDataSet);df[,1] <- df[,1]+1;OutputDataSet <-df'
        ,@input_data_1 = N'select * from tblEmployee where EmployeeId=@empid AND Name=@NewName AND DOB=@NewDOB'
        ,@params = N'@empid Int OUTPUT,@NewName varchar(50) OUTPUT,@NewDOB Varchar(50) OUTPUT'
        ,@empid = @empid2
        ,@NewName =@Name1
        ,@NewDOB=@DOB1
        WITH RESULT SETS (([Col1] Int, [Col2] varchar(50), [Col3] varchar(50), Col4 varchar(50),Col5 varchar(50),Col6 varchar(50)));

    End

    -- EXEC dbo.spGetAllEmployees @empid2 ='1', @Name1='Nabi',@DOB1='2017-04-01';