Skip to Main Content
  • Questions
  • after update count of executed updates

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Kerstin.

Asked: February 16, 2017 - 1:51 pm UTC

Last updated: February 22, 2017 - 1:14 am UTC

Version: 11.0.2.3

Viewed 10K+ times! This question is

You Asked

I have a script executed in SQL*PLUS command line that should give out the summ of all real updated rows from the WHERE clause after all update executes simiilar as on execute update command directely in SQL*Plus with "1 row updated".

But the following code is not returning to the SQL> prompt.
How do I get the sum of the real updates rows in the WHERE clause printed in the spool output ?

--Variblensubst
--@ W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\ZD_ZZB-update-nicht-historisch-Merker-M-2017-Q1.sql
set define off;

set COLSEP '|'
set verify off
set feedback off
set trimspool on
set trimout on
--output welche SQL statements
set echo on
--Variable zum Zählen der Zeilen
DECLARE @rowsreturned INT
SET @rowsreturned = @@ROWCOUNT


spool W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\sätze-zum-modifizieren-nicht-historisch-2017-Q1.txt


--update nicht historische Sätze

update ZD_ZZB set ZZBV='010',ZZBPLZ='30890', ZZBBTEXZ='Commerzbank Barsinghausen ', ZZBPAN='24250', ZZBNBLZ='00000000' where ZZBS = '056053' and ZZBDATB='20991231';
update ZD_ZZB set ZZBV='010',ZZBPLZ='49406', ZZBBTEXZ='VB Diepholz-Barnstorf ', ZZBPAN='62386', ZZBNBLZ='00000000' where ZZBS = '025945' and ZZBDATB='20991231';
........


--select zum Zählen der Zeilen
SELECT rowsreturned

--Einstellungen für spool zurücknehmen
spool off

set verify on
set feedback on

and Connor said...

You have two options here

1) parse the output, eg

SQL> spool /tmp/output.log
SQL> drop table t purge;

Table dropped.

SQL> create table t as select * from dba_objects;

Table created.

SQL> update t set object_id = object_id+1 where owner = 'SYS';

42753 rows updated.

SQL> commit;

Commit complete.

SQL> update t set object_id = object_id+1 where owner = 'SCOTT';

17 rows updated.

SQL> commit;

Commit complete.

SQL> spool off

grep "rows updated" output.log | gawk "{s+=$1}END{print s}"
42770
<code>

2) Use PL/SQL as a wrapper

<code>
declare
  tot int := 0;
begin
update ZD_ZZB set ...; 
tot := tot + sql%rowcount;

update ZD_ZZB set ...; 
tot := tot + sql%rowcount;

dbms_output.put_line('Total rows = '||tot);
end;
/

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

count really updated rows

Kerstin Volkenand, February 17, 2017 - 1:00 pm UTC

I changed the script, but it is still not running.

SQL pluzs commmandline call:
@ W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\ZD_ZZB-update-nicht-historisch-Merker-M-2017-Q1.sql

script:
--@ W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\ZD_ZZB-update-nicht-historisch-Merker-M-2017-Q1.sql
--keine Variblensubstitution wie z.B &
set define off
--wegen Umlautedarstellung
SET NLS_LANG=GERMAN_GERMANY.WE8PC850

set COLSEP '|'
set verify off
set feedback off
set trimspool on
set trimout on
--output welche SQL statements
set echo on
--Variable zum Zählen der Zeilen
--code
declare tot int := 0;
begin


spool W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\saetze-zum-modifizieren-nicht-historisch-2017-Q1.txt


--update nicht historische Sätze

update ZD_ZZB set ZZBV='010',ZZBPLZ='30890', ZZBBTEXZ='Commerzbank Barsinghausen', ZZBPAN='24250', ZZBNBLZ='00000000' where ZZBS = '056053' and ZZBDATB='20991231';
tot:=tot + sql%rowcount;
update ZD_ZZB set ZZBV='010',ZZBPLZ='49406', ZZBBTEXZ='VB Diepholz-Barnstorf', ZZBPAN='62386', ZZBNBLZ='00000000' where ZZBS = '025945' and ZZBDATB='20991231';
tot:=tot+sql%rowcount;
update ZD_ZZB set ZZBV='010',ZZBPLZ='30890', ZZBBTEXZ='Commerzbank Barsinghausen', ZZBPAN='28250', ZZBNBLZ='00000000' where ZZBS = '014871' and ZZBDATB='20991231';
tot:=tot + sql%rowcount;

--returnrowanzahl
dbms_output.put_line('Totalrows='||tot);
end;

--Einstellungenfürspoolzurücknehmen
spool off

set verify on
set feedback on
Connor McDonald
February 18, 2017 - 4:29 am UTC

Add

set serveroutput on

to your script to let SQLPlus know to return the output from dbms_output.

MS SQL Scripts

Rajeshwaran, Jeyabal, February 17, 2017 - 2:30 pm UTC

BTW, this is not Oracle script, those are from MS SQL scripts.

DECLARE @rowsreturned INT 
SET @rowsreturned = @@ROWCOUNT 

count really updated rows

Kerstin Volkenand, February 20, 2017 - 7:55 am UTC

could you send a complete sample for Oracle with spool file and get really updated rows printed ?
Connor McDonald
February 20, 2017 - 1:23 pm UTC

My first answer does that, no ?


really updated rows

Kerstin Volkenand, February 20, 2017 - 1:33 pm UTC

I'm not sure where to set the SET commmand and how I get the sum after the update Statements.
So I Need a full SQL sample.
Connor McDonald
February 21, 2017 - 1:47 am UTC

SQL> spool /tmp/output.log
SQL> drop table t purge;

Table dropped.

SQL> create table t as select * from dba_objects;

Table created.

SQL> set serverout on
SQL> declare
  2    s int := 0;
  3  begin
  4    update t set object_id = object_id+1 where owner = 'SYS';
  5    s := s + sql%rowcount;
  6    commit;
  7    update t set object_id = object_id+1 where owner = 'SCOTT';
  8    s := s + sql%rowcount;
  9    commit;
 10    dbms_output.put_line('Rows = '||s);
 11  end;
 12  /
Rows = 42780

PL/SQL procedure successfully completed.

SQL> spool off


really updated rows

Kerstin Volkenand, February 21, 2017 - 7:50 am UTC

I miss the
DECLARE @rowsreturned INT
SET @rowsreturned = @@ROWCOUNT

it is the same as:
declare s int := 0;

and is there any difference in behavior when I call from SQL*Plus Window ?
@ W:\SG-323\TeamZentraleKatalogredaktion\Bankleitzahlen\ZZZTest-ZZB\2017-Q1\ZD_ZZB-update-nicht-historisch-Merker-M-2017-Q1.sql

I always have Oracle SQL.
Connor McDonald
February 22, 2017 - 1:14 am UTC

I'm really sorry - I dont understand what you're asking/saying here

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here