Determining the number of rows affected by DML commands¶
After a DML command is executed (excluding the TRUNCATE TABLE command), Snowflake Scripting sets the following global variables. You can use these variables to determine if the last DML statement affected any rows.
Variable |
Description |
|---|---|
|
Number of rows affected by the last DML statement. This is equivalent to |
|
|
|
|
Note
The 2025_01 behavior change bundle changes the behavior of these variables. When the bundle is enabled, the variables return NULL when a non-DML statement is executed after the last DML statement in a Snowflake Scripting block or stored procedure. The bundle is enabled by default. For more information about the behavior change, see Snowflake Scripting: Changes to global variables.
If the bundle is disabled, you can enable it in your account by executing the following statement:
To disable the bundle, execute the following statement:
The examples in this section use the following table:
The following example uses the SQLROWCOUNT variable to return the number of rows affected by the last
DML statement (the INSERT statement).
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
The following example uses the SQLFOUND and SQLNOTFOUND variables to return the number of rows affected by the
last DML statement (the UPDATE statement).
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
When the anonymous block runs, the SQLFOUND variable is true because the UPDATE statement updates two rows.
Query the table to see the current values:
Run the same anonymous block again, and the results are the following:
The UPDATE statement is executed because there is a value in the table that is greater than
2. That is, the IF condition is satisfied.The
SQLNOTFOUNDvariable istruebecause no rows are updated. The UPDATE statement doesn’t update any rows because none of the values in the table are less than3(specified in the WHERE clause).
The query returns the following output:
Now, update the table to set all of the values to 1:
Run the same anonymous block again, and the UPDATE statement isn’t executed because none of the values
in the table are greater than 2. That is, the IF condition isn’t satisfied, so the UPDATE statement
doesn’t execute.