Invantive 문서Invantive Cloud의 일부
Invantive 지원 포럼로그인 / 회원가입

SQLROWCOUNT

구문

%%{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))

목적

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.
  • update and delete: the number of rows updated or deleted.
  • create table and create or replace table, with or without as 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 a create or replace table ... as select whose query returns no rows sets SQLROWCOUNT to 1.
  • create table if not exists on an existing table: 0, since nothing is created.
  • drop table: 1 for the table plus the number of rows it held. drop table if exists on 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, so create index gives 0. Available from release 27.0.97; before, execute native left SQLROWCOUNT at 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.

예시

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

-------------------