Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 9259164

Browse files
authored
Merge pull request #19889 from arvindshmicrosoft/arvindsh/clarify-output-param-in-spees
Fix the sample for OUTPUT param handling in sp_execute_external_script
2 parents 1be6f54 + 0f370de commit 9259164

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

docs/machine-learning/deploy/modify-r-python-code-to-run-in-sql-server.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,25 @@ How much you change your code depends on whether you intend to submit the code f
7272

7373
+ When running code in a stored procedure, you can pass through multiple **scalar** inputs. For any parameters that you want to use in the output, add the **OUTPUT** keyword.
7474

75-
For example, the following scalar input `@model_name` contains the model name, which is also output in its own column in the results:
75+
For example, the following scalar input `@model_name` contains the model name, which is also later modified by the R script, and output in its own column in the results:
7676

7777
```sql
78-
EXECUTE sp_execute_external_script @model_name = "DefaultModel" OUTPUT
79-
,@language = N'R'
80-
,@script = N'R code here'
78+
-- declare a local scalar variable which will be passed into the R script
79+
DECLARE @local_model_name AS NVARCHAR (50) = 'DefaultModel';
80+
81+
-- The below defines an OUTPUT variable in the scope of the R script, called model_name
82+
-- Syntactically, it is defined by using the @model_name name. Be aware that the sequence
83+
-- of these parameters is very important. Mandatory parameters to sp_execute_external_script
84+
-- must appear first, followed by the additional parameter definitions like @params, etc.
85+
EXECUTE sp_execute_external_script @language = N'R', @script = N'
86+
model_name <- "Model name from R script"
87+
OutputDataSet <- data.frame(InputDataSet$c1, model_name)'
88+
, @input_data_1 = N'SELECT 1 AS c1'
89+
, @params = N'@model_name nvarchar(50) OUTPUT'
90+
, @model_name = @local_model_name OUTPUT;
91+
92+
-- optionally, examine the new value for the local variable:
93+
SELECT @local_model_name;
8194
```
8295

8396
+ Any variables that you pass in as parameters of the stored procedure [sp_execute_external_script](../../relational-databases/system-stored-procedures/sp-execute-external-script-transact-sql.md) must be mapped to variables in the code. By default, variables are mapped by name. All columns in the input dataset must also be mapped to variables in the script.

0 commit comments

Comments
 (0)