IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Trouble in executing query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Shweta12j

New User


Joined: 10 May 2010
Posts: 32
Location: Mumbai

PostPosted: Thu Mar 21, 2013 11:41 am
Reply with quote

Hello All ,

I am having a specific requirement wherein I need to fetch data from a table as :

Select Department , Salary , EmpNo , Project from Employee ;

Now my requirment is out of the above 4 columns ( mentioned in select clause ) any one of the field may have a valid value and another may have spaces for eg.

Department = D01
Empno = Spaces
Project = ABC
Salary = Spaces

My requirement is to fetch only those records for which Department is D01 and Proeject is ABC.

This data may keep on varying. For next record I may have Empno = ABC123 and Project = ABN and other fields as spaces ..this way various combinations is possible.

Now I am trying to use below query :

Select Department , Salary , EmpNo , Project from Employee
where
(Department = 'Valid value ' or Department > Spaces ) and
(Salary = 'Valid Value ' or Salary > Spaces ) and
(Empno ='Valid Value ' or Empno > spaces) and
(Project =' Valid Value ' or Project > spaces) ;

Above query is fetching all the records instead of fetching records on specific data present in host variables.

Can anyone please suggest a way to fullfill this requirement.

Regards,
Shweta.
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 792
Location: Chennai, India

PostPosted: Thu Mar 21, 2013 11:56 am
Reply with quote

Simply you can try with a Cursor to select all the rows (possibly, multi row-fetch) and fetch the rows & apply your space-checking logic inside the program.
Back to top
View user's profile Send private message
Shweta12j

New User


Joined: 10 May 2010
Posts: 32
Location: Mumbai

PostPosted: Thu Mar 21, 2013 12:12 pm
Reply with quote

Hi Gnanas N ,

I used the same query mentioned in my above post in cursor however I am not getting desired results as per the values mentioned in host variable.

I also examined an option of fetching all the rows in working storage variable and then comparing each..but then this won't work as a table would have load of data.

Regards,
Shweta.
Back to top
View user's profile Send private message
Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 792
Location: Chennai, India

PostPosted: Thu Mar 21, 2013 12:31 pm
Reply with quote

Please post some sample input values and the result of the query.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Thu Mar 21, 2013 1:59 pm
Reply with quote

it should be :
Code:
(:HV = Spaces or Department = :HV ) and ...


I'm not sure coalesce accept host-variabes as input, and I haven't tried this, because it's been ages since I compiled a program here, but this might also work :
Code:
(Department = coalesce(:HV:HVind,Department) )
as long as you fill :HVind with -1 when :HV = spaces
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts executing XCTL command in COBOL witho... CICS 10
Search our Forums:

Back to Top