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 > union in stored procedure

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-08-07, 12:04
wishlister wishlister is offline
Registered User
 
Join Date: Feb 2007
Posts: 42
union in stored procedure

how will i perform UNION of SELECT statements in stored procedure?
Reply With Quote
  #2 (permalink)  
Old 03-08-07, 12:18
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Use the UNION Keyword as you would use in SQL

Sathyaram

PS: Not sure if the response helps you ... But, with the info you have given, am unable to give any better response
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 03-08-07, 12:29
wishlister wishlister is offline
Registered User
 
Join Date: Feb 2007
Posts: 42
it didn;t work maybe because there is FOR before my select statement. can u give me an example for this.. union of 2 statements with cursor also! tnx
Reply With Quote
  #4 (permalink)  
Old 03-08-07, 12:32
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
It will be more useful if you can provide an example of what you are looking for ...

Also, give your OS, db2 version informaions

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #5 (permalink)  
Old 03-08-07, 12:42
wishlister wishlister is offline
Registered User
 
Join Date: Feb 2007
Posts: 42
db2 ESE

for
SELECT <statement>;
OPEN Cursor1;
FETCH <statement>;
WHILE SQLSTATE = '00000' Do
INSERT INTO <statement>;
FETCH <statement>;
END WHILE;
close Cursor1;
UNION
for
for
SELECT <statement>;
OPEN Cursor2;
FETCH <statement>;
WHILE SQLSTATE = '00000' Do
INSERT INTO <statement>;
FETCH <statement>;
END WHILE;
close Cursor2;

//An unexpected token "for" was found
Reply With Quote
  #6 (permalink)  
Old 03-08-07, 12:48
wishlister wishlister is offline
Registered User
 
Join Date: Feb 2007
Posts: 42
another question..
cursor1 is for the first table and cursor2 for the second table. is it right to use different cursors? or can i use only one cursor?
Reply With Quote
  #7 (permalink)  
Old 03-09-07, 04:08
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
A union combines two subselects into one. So you would have:
Code:
DECLARE ... CURSOR FOR
   SELECT ... FROM ... WHERE ...
   UNION
   SELECT ... FROM ... WHERE ...;
OPEN ...;
WHILE ( ... ) DO
   FETCH ...
END WHILE;
CLOSE ...;
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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