In a task graph, a task can call this function to set a return value.
Another task that identifies this task as the predecessor task (using the AFTER keyword in the task definition)
can retrieve the return value set by the predecessor task using SYSTEM$GET_PREDECESSOR_RETURN_VALUE.
Create a task that sets a return value. Create a second, child task that runs after the predecessor task has completed.
The child task retrieves the return value set by the predecessor task (by calling SYSTEM$GET_PREDECESSOR_RETURN_VALUE) and inserts it into a table row:
-- Create a table to store the return values.CREATEORREPLACETABLE return_values_table (strVARCHAR);-- Create a task that sets the return value for the task.CREATETASK set_return_value_task
WAREHOUSE= return_task_wh
SCHEDULE='1 MINUTE'ASCALLSYSTEM$SET_RETURN_VALUE('The quick brown fox jumps over the lazy dog');-- Create a task that identifies the first task as the predecessor task and retrieves the return value set for that task.CREATETASK get_return_value_task
WAREHOUSE= return_task_wh
AFTER set_return_value_task
ASINSERTINTO return_values_table VALUES(SYSTEM$GET_PREDECESSOR_RETURN_VALUE());-- Note that if there are multiple predecessor tasks that are enabled, you must specify the name of the task to retrieve the return value for that task.CREATETASK get_return_value_by_pred_task
WAREHOUSE= return_task_wh
AFTER set_return_value_task
ASINSERTINTO return_values_table VALUES(SYSTEM$GET_PREDECESSOR_RETURN_VALUE('get_return_value_task'));-- Resume task (using ALTER TASK ... RESUME).-- Wait for task to run on schedule.SELECTDISTINCT(str)FROM return_values_table;+-----------------------------------------------+|STR|+-----------------------------------------------+| The quick brown fox jumps over the lazy dog |+-----------------------------------------------+SELECTDISTINCT(RETURN_VALUE)FROMTABLE(information_schema.task_history())WHERE RETURN_VALUE ISNOTNULL;+-----------------------------------------------+| RETURN_VALUE |+-----------------------------------------------+| The quick brown fox jumps over the lazy dog |+-----------------------------------------------+
Example 2: Call by using a separate stored procedure¶
Similar to the first example, but set the return value for the task and retrieve it by calling separate stored procedures:
-- Create a table to store the return values.CREATEORREPLACETABLE return_values_sp (strVARCHAR);-- Create a stored procedure that sets the return value for the task.CREATEORREPLACEPROCEDURE set_return_value_sp()RETURNSSTRINGLANGUAGEJAVASCRIPTEXECUTEASCALLERAS$$
var stmt = snowflake.createStatement({sqlText:`CALL SYSTEM$SET_RETURN_VALUE('The quick brown fox jumps over the lazy dog');`});
var res = stmt.execute();
$$;-- Create a stored procedure that inserts the return value for the predecessor task into the 'return_values_sp' table.CREATEORREPLACEPROCEDURE get_return_value_sp()RETURNSSTRINGLANGUAGEJAVASCRIPTEXECUTEASCALLERAS$$
var stmt = snowflake.createStatement({sqlText:`INSERT INTO return_values_sp VALUES(SYSTEM$GET_PREDECESSOR_RETURN_VALUE());`});
var res = stmt.execute();
$$;-- Create a task that calls the set_return_value_sp stored procedure.CREATETASK set_return_value_t
WAREHOUSE=warehouse1
SCHEDULE='1 MINUTE'ASCALL set_return_value_sp();-- Create a task that calls the get_return_value stored procedure.CREATETASK get_return_value_t
WAREHOUSE=warehouse1
AFTER set_return_value_t
ASCALL get_return_value_sp();-- Resume task.-- Wait for task to run on schedule.SELECTDISTINCT(str)FROM return_values_sp;+-----------------------------------------------+|STR|+-----------------------------------------------+| The quick brown fox jumps over the lazy dog |+-----------------------------------------------+SELECTDISTINCT(RETURN_VALUE)FROMTABLE(information_schema.task_history())WHERE RETURN_VALUE ISNOTNULL;+-----------------------------------------------+| RETURN_VALUE |+-----------------------------------------------+| The quick brown fox jumps over the lazy dog |+-----------------------------------------------+
Example 3: Use a variable to set the return value¶
The following example demonstrates how to dynamically generate a return value based on the task’s execution and set the return value by using a variable. In this example, the task loads data from a stream into a landing table and sets the return value to indicate the number of rows loaded:
CREATEORREPLACETASK load_raw_data
WAREHOUSE='WH'WHENSYSTEM$STREAM_HAS_DATA('NEW_WEATHER_DATA')ASDECLARE
rows_loaded NUMBER;
result_string VARCHAR;BEGININSERTINTO raw_weather_data (-- our landing table
row_id)SELECT
row_id
FROM
new_weather_data -- our source stream;-- to see the number of rows loaded in the UI
rows_loaded :=(SELECT$1FROMTABLE(RESULT_SCAN(LAST_QUERY_ID())));
result_string :=:rows_loaded||' rows loaded into RAW_WEATHER_DATA';-- show result string as task return valueCALLSYSTEM$SET_RETURN_VALUE(:result_string);END;