How to pass two sql tables as input parameter for r codes in SQL Server

后端 未结 1 568
渐次进展
渐次进展 2021-01-07 02:54

I am running r codes in SQL Server. I have two tables in SQL Server database that I would like to pass as input data set into the R code. I can use @input_data_1

相关标签:
1条回答
  • 2021-01-07 03:29

    You can use R's built in serialization to accomplish this in the SQL Server 2016. It's definitely verbose, however, it does work.

    Example, setup two separate input temp tables (they cannot be table variables, unfortunately):

     CREATE TABLE #Table1
        (
            [StrCol] NVARCHAR( 50 ),
            [IntCol] INT
        );
    
        INSERT INTO
            #Table1
            (
                [StrCol],
                [IntCol]
            )
            VALUES
            ( N'testing data 1', 1 ),
            ( N'testing data 2', 2 )
        ;
    
     CREATE TABLE #Table2
        (
            [StrCol] NVARCHAR( 50 ),
            [IntCol] INT
        );
    
        INSERT INTO
            #Table2
            (
                [StrCol],
                [IntCol]
            )
            VALUES
            ( N'more testing data 1', 5 ),
            ( N'more testing data 2', 6 )
        ;
    

    From here, you can create VARBINARY types to hold the serialized results translated in R.

    DECLARE
        @Table1_Input NVARCHAR( MAX ) = 'SELECT * FROM #Table1;',
        @Table2_Input NVARCHAR( MAX ) = 'SELECT * FROM #Table2;',
        @Table1_Data VARBINARY( MAX ),
        @Table2_Data VARBINARY( MAX );
    
    EXECUTE sp_execute_external_script
        @language = N'R',
        @script = N'
    
            if( nrow(InputDataSet) == 0 )
                stop("Invalid data passed in")
    
            # Read in the sql table, serialize it to an output string
            Output <- serialize(InputDataSet, NULL)
        ',
        @input_data_1 = @Table1_Input,
        @params = N'@Output VARBINARY( MAX ) OUTPUT',
        @Output = @Table1_Data OUTPUT;
    
    EXECUTE sp_execute_external_script
        @language = N'R',
        @script = N'
    
            if( nrow(InputDataSet) == 0 )
                stop("Invalid data passed in")
    
            # Read in the sql table, serialize it to an output string
            Output <- serialize(InputDataSet, NULL)
        ',
        @input_data_1 = @Table2_Input,
        @params = N'@Output VARBINARY( MAX ) OUTPUT',
        @Output = @Table2_Data OUTPUT;
    

    Finally,

    EXECUTE sp_execute_external_script
        @language = N'R',
        @script = N'
    
            table1 <- unserialize(Table1_Data)
            table2 <- unserialize(Table2_Data)
    
            OutputDataSet <- rbind(table1, table2)
        ',
        @params = N'@Table1_Data VARBINARY(MAX), @Table2_Data VARBINARY(MAX)',
        @Table1_Data = @Table1_Data,
        @Table2_Data = @Table2_Data
        WITH RESULT SETS (( [Col1] NVARCHAR( 50 ), [Col2] INT ));
    

    Results:

    Col1    Col2
    testing data 1
    testing data 2
    more testing data 5
    more testing data 6
    
    0 讨论(0)
提交回复
热议问题