DML コマンドの影響を受ける行数の決定

DML コマンド が実行された後、Snowflakeスクリプトは次のグローバル変数を設定します。これらの変数を使用して、最後の DML ステートメントが行に影響を与えたかどうかを判別できます。

変数

説明

SQLROWCOUNT

最後の DML ステートメントの影響を受けた行の数。

これは、 JavaScript ストアドプロシージャの getNumRowsAffected() と同等です。

SQLFOUND

最後の DML ステートメントが1つ以上の行に影響を与えた場合は true

SQLNOTFOUND

最後の DML ステートメントがゼロ行に影響を与えた場合は true

このセクションの例では、次のテーブルを使用します。

CREATE OR REPLACE TABLE my_values (value NUMBER);
Copy

次の例では、 SQLROWCOUNT 変数を使用して、最後の DML ステートメント(この例では INSERT ステートメント)の影響を受ける行数を返します。SELECT ステートメントは DML ステートメントではなく、 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

注: SnowSQLClassic Consoleexecute_stream または execute_string メソッドを Python Connector コードで使用している場合は、代わりにこの例を使用してください(SnowSQL、 Classic Console、Python ConnectorでSnowflakeスクリプトを使用する を参照)。

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 |
+-----------------+

次の例では、 SQLFOUND 変数と SQLNOTFOUND 変数を使用して、最後の DML ステートメント(UPDATE ステートメント)の影響を受ける行数を返します。前の例の場合と同様に、 SELECT ステートメントは SQLFOUND 変数と 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

注: SnowSQLClassic Consoleexecute_stream または execute_string メソッドを Python Connector コードで使用している場合は、代わりにこの例を使用してください(SnowSQL、 Classic Console、Python ConnectorでSnowflakeスクリプトを使用する を参照)。

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. |
+-----------------+