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 > Can cursor join with table ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-27-11, 07:45
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Question Can cursor join with table ?

Dear Sir,

I want to know that cursor can join with table in the same sql ? Sample as below.

declare c_1 cursor with hold for
select case_num, amount from table1;

declare c2 cursor with hold for
select a.name, b.case_num, b.amount from table2 a, c_1 b where a.name = b.name (+);


Thanks !
Reply With Quote
  #2 (permalink)  
Old 10-27-11, 09:01
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
as long as the select is valid, it can also be defined for a cursor..
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #3 (permalink)  
Old 10-27-11, 09:18
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
amcc38,

Why do you want to do such troublesome/useless way?
It is not necessary to delare c_1.
You can join table1 with table2 in c_2.

Quote:
where a.name = b.name (+)
This syntax is valid only in Oracle or (may be valid) in DB2 setting compatibility vector to Oracle.
Use of newer syntax using OUTER JOIN keyword is recommended.
Reply With Quote
  #4 (permalink)  
Old 10-27-11, 12:32
amcc38 amcc38 is offline
Registered User
 
Join Date: Oct 2011
Posts: 12
Quote:
Originally Posted by tonkuma View Post
amcc38,

Why do you want to do such troublesome/useless way?
It is not necessary to delare c_1.
You can join table1 with table2 in c_2.


This syntax is valid only in Oracle or (may be valid) in DB2 setting compatibility vector to Oracle.
Use of newer syntax using OUTER JOIN keyword is recommended.
Actually, the c_1 will be used as master table and it will be joined with other tables and form several cursor for further use. If c_1 extraction logic has been changes, needn't to modify each cursor. Logically, I think it works, right ?

Anyway, thanks !
Reply With Quote
  #5 (permalink)  
Old 10-27-11, 14:15
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Actually, the c_1 will be used as master table and it will be joined with other tables and form several cursor for further use. If c_1 extraction logic has been changes, needn't to modify each cursor. Logically, I think it works, right ?
Usually, using multiple cursor is a sign of bad design of DB access(with SQL).
I think if you showed whole procedure using these cursors,
I could show you the simplified design with reducing number of cursors(usually to one cursor).

Anyway, you can go your way.
Reply With Quote
  #6 (permalink)  
Old 10-27-11, 15:56
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by amcc38 View Post
declare c2 cursor with hold for
select a.name, b.case_num, b.amount from table2 a, c_1 b where a.name = b.name (+);
No, this won't work in DB2. The "(+)" notation is (legacy) Oracle notation, not standard SQL. Do rather something like:
Code:
declare c2 cursor with hold for
select b.case_num, b.amount, a.name
from table2 a LEFT JOIN table1 b ON a.name = b.name
Moreover, this cursor makes cursor c1 unnecessary: it returns exactly the same results in the first two columns as c1 (be it possibly repeatedly, and/or both NULL).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/

Last edited by Peter.Vanroose; 10-27-11 at 16:02.
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