MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

union in stored procedure
Goto page 1, 2  Next
 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 12:05 pm    Post subject: union in stored procedure Reply with quote

how will i perform UNION of SELECT statements in stored procedure?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 12:19 pm    Post subject: Reply with quote

wishlister,

The same way you code in regular programs . check this link which explains union with examples

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/4.3?DT=20010718164132

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 12:25 pm    Post subject: Reply with quote

can u give me an example? union of 2 select statements where there is cursor also.. ty! the samples in the link did not help
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 12:33 pm    Post subject: Reply with quote

wishlister wrote:
can u give me an example? union of 2 select statements where there is cursor also.. ty! the samples in the link did not help


what do you mean it did not help? Did you get an error? if so what is the error?
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 12:43 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 12:48 pm    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 12:50 pm    Post subject: Reply with quote

wishlister,

you just need 1 cursor

try this

Code:

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

_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 12:58 pm    Post subject: Reply with quote

tnx! i know it will work but now, im having problem with my field names..

SELECT no, NAME, type, rate, hrs from HR
UNION
SELECT no, NAME, type, sal, bonus from MIS;

//error: The data types of corresponding columns are not compatible in a fullselect that includes a set operator or in the multiple rows of a VALUES clause of an INSERT or fullselect.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 1:04 pm    Post subject: Reply with quote

wishlister,

If the attributes are different then convert them to a common data attribute.

ex:

Code:

SELECT no, NAME, type, char(rate), char(hrs) from HR
UNION
SELECT no, NAME, type, char(sal), char(bonus) from MIS;


Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 1:08 pm    Post subject: Reply with quote

oic.. now, it runs but when i call the stored proc it produces an error: A value cannot be assigned to a host variable in the SELECT, VALUES, or FETCH statement because the data types are not compatible.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 1:14 pm    Post subject: Reply with quote

wishlister wrote:
oic.. now, it runs but when i call the stored proc it produces an error: A value cannot be assigned to a host variable in the SELECT, VALUES, or FETCH statement because the data types are not compatible.


wishlister,

how is your target table defined? is it Integer or decimal? Based on the target table definitons use the function like CHAR or Int or DECIMAL

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Mar 08, 2007 1:14 pm    Post subject: Reply with quote

did you insure that the data types for your host variables for rate,hrs,sal,and bonus are x type?
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 1:24 pm    Post subject: Reply with quote

what do u mean by x type?
Back to top
View user's profile Send private message
wishlister
Beginner


Joined: 08 Mar 2007
Posts: 16
Topics: 3

PostPosted: Thu Mar 08, 2007 1:40 pm    Post subject: Reply with quote

kolusu wrote:
wishlister wrote:
oic.. now, it runs but when i call the stored proc it produces an error: A value cannot be assigned to a host variable in the SELECT, VALUES, or FETCH statement because the data types are not compatible.


wishlister,

how is your target table defined? is it Integer or decimal? Based on the target table definitons use the function like CHAR or Int or DECIMAL

Kolusu


just want to remind you that sal & bonus will not be placed under the column rate and hrs. they are another column, meaning if the employee is under MIS, his rate & hrs columns will be blank.
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12375
Topics: 75
Location: San Jose

PostPosted: Thu Mar 08, 2007 1:47 pm    Post subject: Reply with quote

wishlister,

post the column defintions of all the tables involved and i will post the sql

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group