If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Feedback on insert/delete/update/merge

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-25-09, 06:36
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
Feedback on insert/delete/update/merge

Hi,

Can someone say how do I know that how many records got updated?

Code:
update 
set abc.emp a
where a.deptno=20;

In oracle we have implicit cursor SQL which tells if I say SQL%ROWCOUNT, do we have any kind of cursor or any functionality in DB2?

This would help me great if you could provide valuable information.

Thanks,
Shahnaz.
Reply With Quote
  #2 (permalink)  
Old 06-25-09, 07:28
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Search this forum - I remember this question was answered a couple of weeks ago.
Reply With Quote
  #3 (permalink)  
Old 06-25-09, 07:40
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Shahnaz, Look up GET DIAGNOSTICS. I believe the syntax is:

DECLARE variable INTEGER

GET DIAGNOSTICS variable = ROW_COUNT
Reply With Quote
  #4 (permalink)  
Old 06-25-09, 08:01
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
thanks for your reply.

GET DIAGNOSTICS works only if we go for creating procedure or function,...

I am in CLP

db2=> update abc.emp set ename='shahnaz';

db2=>


In this level, how can I use GET DIAGNOSTICS ? I think we need to declare variable here, since I am new bee to this DB2 arena it would be great help for me if you resolve this issue.

Thanks,
Shahnaz.
Reply With Quote
  #5 (permalink)  
Old 06-25-09, 19:17
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You can simply query the results of the UPDATE statement:
Code:
SELECT COUNT(*)
FROM NEW TABLE ( UPDATE ... )
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 06-26-09, 01:42
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
I tried by giving

Code:
select count(*) from sysibm.sysdumm1
(update abc.emp set ename='shahnaz')
but it throws me error!

Quote:
SQL0104N An unexpected token "sysibm.sysdummy1" was found following "select
count(*) from". Expected tokens may include: "<new_or_old_or_final_table>".
SQLSTATE=42601
Can anybody suggest about this issue?

Thanks,
Shahnaz.
Reply With Quote
  #7 (permalink)  
Old 06-26-09, 02:01
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
select count(*) from new table
(update abc.emp set ename='shahnaz') as x
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #8 (permalink)  
Old 06-26-09, 02:34
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
thanks Satyaram .

It is doing fine for insert,update and delete operations...

I tried using for merge statement to find out how many records update and even how many records inserted but it gives me error!

Code:
select count(*) from new table
(
merge into abc.emp_temp a
using abc.emp b
on(a.empno=b.empno)
when matched then
update set
ename=b.ename
)
Can you please suggest, what would be the work around?

thanks,
Shahnaz.
Reply With Quote
  #9 (permalink)  
Old 06-26-09, 03:19
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If you use the -a option, the entire SQLCA will be output. After INSERT, UPDATE, DELETE, or MERGE, SQLERRD(3) contains the actual number of rows that qualified for the operation.

for example:

db2 -tvaf filename

or

db2 -a "update set abc.emp a where a.deptno=20"
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #10 (permalink)  
Old 06-26-09, 05:51
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
Fantastic Marcus.

It is really works. I got a sql file where I have select statements and insert statements; if I use db2 -tvaf then it is going to work on all the statement but I need to restrict only to insert statement.


Thanks,
Shahnaz.
Reply With Quote
  #11 (permalink)  
Old 06-26-09, 07:09
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
You can also turn the 'a' switch on/off using a stmt

update command options using a on
update command options using a off

if you put the ON stmt before the inserts and then after you have done the insert, turn it OFF .. If you are using these commands, you do not need the 'a' in -tvaf

HTH
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #12 (permalink)  
Old 06-29-09, 03:05
shahnazurs shahnazurs is offline
Registered User
 
Join Date: May 2005
Posts: 25
But I tried the following statement

Code:
merge into abc.emp_temp a
using abc.emp b
on(a.empno=b.empno)
when matched then
update set
ename=b.ename
using a on;
but it is giving error! Can you please correct me if I missed anything. Hopefully DB2 engine might have confusing by looking at two "using"!

Thanks,
Shahnaz.
Reply With Quote
  #13 (permalink)  
Old 06-29-09, 12:39
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
update command options using a on;

merge into abc.emp_temp a
using abc.emp b
on(a.empno=b.empno)
when matched then
update set
ename=b.ename;

update command options using a off;
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On