Como determinar o número de linhas afetadas por comandos DML

Após a execução de cada comando DML, o Script Snowflake define as seguintes variáveis globais. Você pode usar essas variáveis para determinar se a última instrução DML afetou alguma linha.

Variável

Descrição

SQLROWCOUNT

Número de linhas afetadas pela última instrução DML.

Isso é equivalente a getNumRowsAffected() em procedimentos armazenados de JavaScript.

SQLFOUND

true se a última instrução DML tiver afetado uma ou mais linhas.

SQLNOTFOUND

true se a última instrução DML tiver afetado zero linhas.

Os exemplos nesta seção usam a seguinte tabela:

CREATE OR REPLACE TABLE my_values (value NUMBER);
Copy

O exemplo a seguir usa a variável SQLROWCOUNT para retornar o número de linhas afetadas pela última instrução DML (a instrução INSERT, neste exemplo). A instrução SELECT não é uma instrução DML e não tem efeito sobre a variável SQLROWCOUNT.

BEGIN

  -- Insert 3 rows into a table.
  INSERT INTO my_values VALUES (1), (2), (3);
  -- SQLROWCOUNT is not affected by statements
  -- that are not DML statements (e.g. SELECT statements).
  SELECT * from my_values;
  -- Returns the number of rows affected by
  -- the last DML statement (the INSERT statement).
  RETURN SQLROWCOUNT;

END;
Copy

Observação: se você estiver usando o método SnowSQL, o Classic Console, execute_stream ou execute_string no código Python Connector, use este exemplo (consulte Uso do Script Snowflake em SnowSQL, Classic Console e conector Python):

EXECUTE IMMEDIATE $$
BEGIN

  -- Insert 3 rows into a table.
  INSERT INTO my_values VALUES (1), (2), (3);
  -- SQLROWCOUNT is not affected by statements
  -- that are not DML statements (e.g. SELECT statements).
  SELECT * from my_values;
  -- Returns the number of rows affected by
  -- the last DML statement (the INSERT statement).
  RETURN SQLROWCOUNT;

END;
$$;
Copy
+-----------------+
| anonymous block |
|-----------------|
|               3 |
+-----------------+

O exemplo a seguir usa as variáveis SQLFOUND e SQLNOTFOUND para retornar o número de linhas afetadas pela última instrução DML (a instrução UPDATE). Como no caso do exemplo anterior, a instrução SELECT não afeta as variáveis SQLFOUND e SQLNOTFOUND.

BEGIN

  -- Update the rows in a table that have values less than 3.
  UPDATE my_values SET value = 4 WHERE value < 3;
  -- SQLFOUND and SQLNOTFOUND are not affected by statements
  -- that are not DML statements (e.g. SELECT statements).
  SELECT * from my_values;
  -- SQLFOUND returns 'true' if the last DML statement
  -- (the UPDATE statement) affected one or more rows.
  IF (SQLFOUND = true) THEN
    RETURN 'Updated ' || SQLROWCOUNT || ' rows.';
  -- SQLNOTFOUND returns 'true' if the last DML statement
  -- (the UPDATE statement) affected zero rows.
  ELSEIF (SQLNOTFOUND = true) THEN
    RETURN 'No rows updated.';
  ELSE
    RETURN 'SQLFOUND and SQLNOTFOUND are not true.';
  END IF;

END;
Copy

Observação: se você estiver usando o método SnowSQL, o Classic Console, execute_stream ou execute_string no código Python Connector, use este exemplo (consulte Uso do Script Snowflake em SnowSQL, Classic Console e conector Python):

EXECUTE IMMEDIATE $$
BEGIN

  -- Update the rows in a table that have values less than 3.
  UPDATE my_values SET value = 4 WHERE value < 3;
  -- SQLFOUND and SQLNOTFOUND are not affected by statements
  -- that are not DML statements (e.g. SELECT statements).
  SELECT * from my_values;
  -- SQLFOUND returns 'true' if the last DML statement
  -- (the UPDATE statement) affected one or more rows.
  IF (SQLFOUND = true) THEN
    RETURN 'Updated ' || SQLROWCOUNT || ' rows.';
  -- SQLNOTFOUND returns 'true' if the last DML statement
  -- (the UPDATE statement) affected zero rows.
  ELSEIF (SQLNOTFOUND = true) THEN
    RETURN 'No rows updated.';
  ELSE
    RETURN 'SQLFOUND and SQLNOTFOUND are not true.';
  END IF;

END;
$$;
Copy
+-----------------+
| anonymous block |
|-----------------|
| Updated 2 rows. |
+-----------------+