Categorias:

Funções do sistema (Informações do sistema)

SYSTEM$SET_RETURN_VALUE

Define explicitamente o valor de retorno para uma tarefa.

Em um DAG de tarefas, uma tarefa pode chamar esta função para definir um valor de retorno. Outra tarefa que identifica esta tarefa como a tarefa predecessora (usando a palavra-chave AFTER na definição da tarefa) pode recuperar o valor de retorno definido pela tarefa predecessora usando SYSTEM$GET_PREDECESSOR_RETURN_VALUE.

Sintaxe

SYSTEM$SET_RETURN_VALUE( '<string_expression>' )
Copy

Argumentos

string_expression

A cadeia de caracteres a ser definida como o valor de retorno. O tamanho da cadeia de caracteres deve ser <= 10 kB (quando codificado em UTF8).

Exemplos

Crie uma tarefa que defina um valor de retorno. Crie uma segunda tarefa, secundária, que seja executada depois que a tarefa anterior tiver sido concluída. A tarefa secundária recupera o valor de retorno definido pela tarefa anterior (chamando SYSTEM$GET_PREDECESSOR_RETURN_VALUE) e o insere em uma linha de tabela:

-- create a table to store the return values.
create or replace table return_values (str varchar);

-- create a task that sets the return value for the task.
create task set_return_value
  warehouse=return_task_wh
  schedule='1 minute' as
  call system$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.
create task get_return_value
  warehouse=return_task_wh
  after set_return_value
  as
    insert into return_values 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.
create task get_return_value_by_pred
  warehouse=return_task_wh
  after set_return_value
  as
    insert into return_values values(system$get_predecessor_return_value('SET_RETURN_VALUE'));

-- resume task (using ALTER TASK ... RESUME).
-- wait for task to run on schedule.

select distinct(str) from return_values;
+-----------------------------------------------+
|                      STR                      |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+

select distinct(RETURN_VALUE)
  from table(information_schema.task_history())
  where RETURN_VALUE is not NULL;

+-----------------------------------------------+
|                  RETURN_VALUE                 |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+
Copy

Similar ao primeiro exemplo, mas definindo o valor de retorno para a tarefa e recuperá-lo chamando procedimentos armazenados separadamente:

-- create a table to store the return values.
create or replace table return_values_sp (str varchar);

-- create a stored procedure that sets the return value for the task.
create or replace procedure set_return_value_sp()
returns string
language javascript
execute as caller
as $$
  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.
create or replace procedure get_return_value_sp()
returns string
language javascript
execute as caller
as $$
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 stored procedure.
create task set_return_value_t
warehouse=warehouse1
schedule='1 minute'
as
  call set_return_value_sp();

-- create a task that calls the get_return_value stored procedure.
create task get_return_value_t
warehouse=warehouse1
after set_return_value_t
as
  call get_return_value_sp();

-- resume task.
-- wait for task to run on schedule.

select distinct(str) from return_values_sp;
+-----------------------------------------------+
|                      STR                      |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+

select distinct(RETURN_VALUE)
  from table(information_schema.task_history())
  where RETURN_VALUE is not NULL;

+-----------------------------------------------+
|                  RETURN_VALUE                 |
+-----------------------------------------------+
|  The quick brown fox jumps over the lazy dog  |
+-----------------------------------------------+
Copy