我正在尝试运行此简单的概念证明:
DECLARE @p1 INT;
DECLARE @p2 INT;
SET @p1 = 1;
SET @p2 = 2;
execute sp_execute_external_script
@language=N'R',
@script=N'inp_dat <- InputDataSet
inp_dat$sum <- inp_dat$p1 + inp_dat$p2
new_dat <- inp_dat',
@input_data_1 = N'SELECT @p1 AS p1, @p2 AS p2',
@output_data_1 = N'newdat';
此处将2个参数作为表传递给R,然后R创建一个计算列,并将R数据帧作为表返回。我目前正在:
Procedure expects parameter '@params' of type 'ntext/nchar/nvarchar'.
有什么想法吗?
PS:
从某种意义上来说,这是有效的:
DECLARE @p1 int;
DECLARE @p2 int;
SET @p1 = 2;
SET @p2 = 3;
DECLARE @inquery NVARCHAR(MAX)
SET @inquery = N'SELECT ' + CAST(@p1 AS NVARCHAR(MAX)) + N' AS p1, ' + CAST(@p2 AS NVARCHAR(MAX)) + N' AS p2;'
DECLARE @sum FLOAT;
EXEC sp_execute_external_script
@language =N'R',
@script=N'
sum <- InputDataSet[1,1] + InputDataSet[1,2]
',
@input_data_1 = @inquery,
@params = N'@sum FLOAT OUTPUT',
@sum = @sum OUTPUT
;
SELECT @sum AS SUM
GO
PPS:
到达那里-唯一的问题-未返回数据框的列名:
DECLARE @p1 int;
DECLARE @p2 int;
SET @p1 = 2;
SET @p2 = 3;
DECLARE @inquery NVARCHAR(MAX)
SET @inquery = N'SELECT ' + CAST(@p1 AS NVARCHAR(MAX)) + N' AS p1, ' + CAST(@p2 AS NVARCHAR(MAX)) + N' AS p2;'
DECLARE @sum FLOAT;
EXEC sp_execute_external_script
@language =N'R',
@script=N'
sum <- InputDataSet[1,1] + InputDataSet[1,2]
InputDataSet$sum <- InputDataSet$p1 + InputDataSet$p2
OutputDataSet <- InputDataSet
',
@input_data_1 = @inquery,
@params = N'@sum FLOAT OUTPUT',
@sum = @sum OUTPUT
;
SELECT @sum AS SUM
GO
答案 0 :(得分:0)
我刚遇到this excellent series,这使我想到了这个解决方案:
DECLARE @p1 int;
DECLARE @p2 int;
SET @p1 = 2;
SET @p2 = 3;
DECLARE @inquery NVARCHAR(MAX)
SET @inquery = N'SELECT ' + CAST(@p1 AS NVARCHAR(MAX)) + N' AS p1, ' + CAST(@p2 AS NVARCHAR(MAX)) + N' AS p2;'
DECLARE @sum FLOAT;
EXEC sp_execute_external_script
@language =N'R',
@script=N'
InputDataSet$sum <- InputDataSet$p1 + InputDataSet$p2
OutputDataSet <- InputDataSet
',
@input_data_1 = @inquery
WITH RESULT SETS(( p1 INT,p2 INT,[sum] INT))
;