Determining the Number of Rows Affected by DML Commands¶
各 DML コマンド が実行された後、Snowflakeスクリプトは次のグローバル変数を設定します。これらの変数を使用して、最後の DML ステートメントが行に影響を与えたかどうかを判別できます。
変数 |
説明 |
---|---|
|
最後の DML ステートメントの影響を受けた行の数。 これは、 JavaScript ストアドプロシージャの |
|
最後の DML ステートメントが1つ以上の行に影響を与えた場合は |
|
最後の DML ステートメントがゼロ行に影響を与えた場合は |
The following example uses the SQLROWCOUNT
variable to return the number of rows affected by the last DML statement (the
INSERT statement, in this example). Note that the SELECT statement is not a DML statement and has no effect on the
SQLROWCOUNT
variable.
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;
$$;
+-----------------+
| anonymous block |
|-----------------|
| 3 |
+-----------------+
次の例では、 SQLFOUND
変数と SQLNOTFOUND
変数を使用して、最後の DML ステートメント(UPDATE ステートメント)の影響を受ける行数を返します。前の例の場合と同様に、 SELECT ステートメントは SQLFOUND
変数と SQLNOTFOUND
変数には影響を与えません。
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;
$$;
+-----------------+
| anonymous block |
|-----------------|
| Updated 2 rows. |
+-----------------+