SQLROWCOUNT
Syntax
%%{init: {
'theme': 'base',
'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
'themeVariables': {
'fontSize': '11px',
'fontFamily': 'Arial'
}
}}%%
flowchart LR
Start((START)) --> SQLROWCOUNT[SQLROWCOUNT]:::quoted
SQLROWCOUNT --> End((END))
Syfte
The SQLROWCOUNT SQL function returns in a PSQL context a long with the number of rows influenced by the last SQL statement. In a SQL context null is returned.
The number depends on the statement:
insert: the number of rows inserted.updateanddelete: the number of rows updated or deleted.create tableandcreate or replace table, with or withoutas select: 1 for the table plus the number of rows created. A table which replaces an existing one counts the same as a new table, so acreate or replace table ... as selectwhose query returns no rows setsSQLROWCOUNTto 1.create table if not existson an existing table: 0, since nothing is created.drop table: 1 for the table plus the number of rows it held.drop table if existson a missing table: 0.execute native: the number of rows the database reports as inserted, updated or deleted. A query, a DDL statement and a platform which reports no such number give the number of rows returned, socreate indexgives 0. Available from release 27.0.97; before,execute nativeleftSQLROWCOUNTat the value of the statement before it.
The databases which report the number of rows changed are those reached through an ANSI SQL driver: Firebird, IBM DB2, MySQL, ODBC, Oracle, PostgreSQL, SQL Server and Teradata.
When an insert, update or delete fails, SQLROWCOUNT in the exception handler of the PSQL block gives the number of rows the statement wrote before the failure, as far as the data container reported them. Rows of a batch which a database rejected as a whole, such as a page of a bulk insert into PostgreSQL or SQL Server, are not counted. Available from the releases of 27.0 and 27.1 built after 1 December 2026; before, a failing statement left SQLROWCOUNT at the value of the statement before it.
Returns: the number of rows influenced by the last SQL statement, or null in a SQL context.
Exempel
SQLROWCOUNT retrieves the number of rows inserted by the insert statement:
begin
insert into table
( col1
)
select value
from range@DataDictionary(5)
;
if sqlrowcount = 0
then
raise_application_error
( 'errorcode'
, 'No rows were loaded.'
);
end if;
end;
The following example checks whether create or replace table ... as select filled the table; the 1 for the table itself is subtracted:
begin
create or replace table Selection@InMemoryStorage
as
select value
from range@DataDictionary(5)
where value > 10
;
if sqlrowcount - 1 = 0
then
raise_application_error
( 'errorcode'
, 'The selection is empty.'
);
end if;
end;
The following example reports how many rows a native delete on a PostgreSQL database removed:
begin
execute native 'delete from audit_log where date_created < now() - interval ''90 days'''
datacontainer 'pg'
;
dbms_output.put_line('Removed ' || to_char(sqlrowcount) || ' audit rows.');
end;
The following example retrieves the number of rows affected by the last SQL statement; the output is empty since null is returned in a SQL context:
select sqlrowcount
-------------------