| |
|
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.
|
 |

10-27-11, 07:45
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 12
|
|
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 !
|
|

10-27-11, 09:01
|
|
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
|
|

10-27-11, 09:18
|
|
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.
|
|

10-27-11, 12:32
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 12
|
|
Quote:
Originally Posted by tonkuma
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 !
|
|

10-27-11, 14:15
|
|
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.
|
|

10-27-11, 15:56
|
|
Registered User
|
|
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
|
|
Quote:
Originally Posted by amcc38
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.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|