Détermination du nombre de lignes affectées par des commandes DML¶
Après l’exécution de chaque commande DML, Snowflake Scripting définit les variables globales suivantes. Vous pouvez utiliser ces variables pour déterminer si la dernière instruction DML a affecté des lignes.
Variable |
Description |
---|---|
|
Nombre de lignes affectées par la dernière instruction DML. Ceci est équivalent à |
|
|
|
|
Les exemples de cette section utilisent le tableau suivant :
CREATE OR REPLACE TABLE my_values (value NUMBER);
L’exemple suivant utilise la variable SQLROWCOUNT
pour retourner le nombre de lignes affectées par la dernière instruction DML (l’instruction INSERT, dans cet exemple). L’instruction SELECT n’est pas une instruction DML et n’a aucun effet sur la variable 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;
Remarque : si vous utilisez SnowSQL, Classic Console, ou la méthode execute_stream
ou execute_string
dans le code Python Connector, utilisez cet exemple à la place (voir Utilisation d’Exécution de scripts Snowflake dans SnowSQL, Classic Console, et le connecteur 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;
$$;
+-----------------+
| anonymous block |
|-----------------|
| 3 |
+-----------------+
L’exemple suivant utilise les variables SQLFOUND
et SQLNOTFOUND
pour retourner le nombre de lignes affectées par la dernière instruction DML (l’instruction UPDATE). Comme dans l’exemple précédent, l’instruction SELECT n’affecte pas les variables SQLFOUND
et 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;
Remarque : si vous utilisez SnowSQL, Classic Console, ou la méthode execute_stream
ou execute_string
dans le code Python Connector, utilisez cet exemple à la place (voir Utilisation d’Exécution de scripts Snowflake dans SnowSQL, Classic Console, et le connecteur 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;
$$;
+-----------------+
| anonymous block |
|-----------------|
| Updated 2 rows. |
+-----------------+