liveBook · Manning

SQLROWCOUNT

As explained in Snowflake Data Engineering

SQLROWCOUNT is a Snowflake Scripting global variable containing the number of rows affected by the most recently executed query.

SQLROWCOUNT is a Snowflake Scripting global variable containing the number of rows affected by the most recently executed query. Use it immediately after a data-change statement to report processing results or write operational metrics. section 4.3.2 section 13.4.1

Use SQLROWCOUNT to make data-processing operations observable:

  • Return a completion message that includes the affected-row count from a stored procedure. section 4.3.2
  • Insert the count into a pipeline log after commands such as COPY INTO, alongside task and run-group identifiers. section 13.4.1
  • Capture the value directly after the command whose result it represents; the logging INSERT follows the COPY command in the task example. section 13.4.1

Returning a load result

After a MERGE, return `Load completed. ` concatenated with SQLROWCOUNT and ` rows affected.` A procedure call can therefore produce a result such as `Load completed. 131 rows affected.`

create or replace task COPY_ORDERS_TASK
  warehouse = BAKERY_WH
  schedule = '10 M'
as
  begin
    copy into EXT.JSON_ORDERS_EXT
    from (
      select 
        $1, 
        metadata$filename, 
        current_timestamp() 
      from @EXT.JSON_ORDERS_STAGE
    )
    on_error = abort_statement;

    insert into PIPELINE_LOG
    select
      SYSTEM$TASK_RUNTIME_INFO('CURRENT_TASK_GRAPH_RUN_GROUP_ID'),
      SYSTEM$TASK_RUNTIME_INFO('CURRENT_ROOT_TASK_NAME'),
      SYSTEM$TASK_RUNTIME_INFO('CURRENT_TASK_NAME'),
      current_timestamp(),
      :SQLROWCOUNT;
  end;

Inspect the task definition to see SQLROWCOUNT recorded in PIPELINE_LOG after COPY INTO, with task runtime metadata. section 13.4.1

How Snowflake Data Engineering covers it

How deep it goes
Defines the variable and demonstrates return and logging patterns, but does not detail broader execution semantics.
Examples
Uses a MERGE procedure returning 131 affected rows and a COPY task inserting the count into PIPELINE_LOG.
What it assumes
Assumes familiarity with Snowflake Scripting statements, stored procedures, tasks, and basic data-loading commands.
What you can do after
Apply it via Snowflake Scripting to return load results and populate operational pipeline logs.
How it connects
Connects Snowflake Scripting with stored procedures, MERGE, tasks, COPY INTO, and data-pipeline logging.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Snowflake Data Engineering. The section links point to the parts of the book it draws on.