如何处理 SQL 过程中的多个表的输出?

How to process output of multiple tables from SQL procedure?

我有一个生成两个表作为输出的过程,但是当我 运行 它时它会生成一个错误。我想将两个表都传递给局部变量。

DECLARE @model_data_stats TABLE (var VARCHAR(150), center FLOAT, scale FLOAT);
DECLARE @model_log_stats TABLE (var VARCHAR(150), zero INT, plusone INT, plustwo INT);
DECLARE @model_logit VARBINARY(MAX);

EXEC sp_execute_external_script @language = N'R',
     @script = N'
       #R script, irrelevant to question
       #the following variables are assigned:
       data_stats <- table
       log_stats <- table
       trained_model <- varbinary(max) data type
     ',
     @input_data_1 = N'SELECT * FROM dbo.Table',
     @params = N'@data_stats TABLE (var VARCHAR(150), center FLOAT, scale FLOAT) OUTPUT,
                 @log_stats TABLE (var VARCHAR(150), zero INT, plusone INT, plustwo INT) OUTPUT,
                 @trained_model VARBINARY(MAX)',
     @data_stats = @model_data_stats OUTPUT,
     @log_stats = @model_log_stats OUTPUT,
     @trained_model = @model_logit OUTPUT;

这是我在 运行 代码时得到的错误:

Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'TABLE'.

我什至不是特别清楚 TABLE 的出现产生了错误,但我猜是输出变量定义的部分(在 @params 之后)。 DECLARE 语句本身 运行 就好了。

我的猜测是我以错误的方式声明了过程的输出。我不确定正确的方法是什么。将不胜感激!

经过进一步研究,我发现我在这里尝试做的事情是不可能的。来自 MS knowledge base:

In SQL Server 2016, the output of R from the stored procedure sp_execute_external_script is limited to a single data.frame or dataset. (This limitation might be removed in future.)

However, you can return outputs of other types in addition to the dataset. For example, you might train a model using a single dataset as input, but return a table of statistics as the output, plus the trained model as an object.