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

How to select perticuler row from a table?


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

New User


Joined: 11 Apr 2006
Posts: 93

PostPosted: Thu Apr 10, 2008 11:59 am
Reply with quote

I have table like this

TBL_NME TBE_DTE FLAG
T1 000000 E
T2 000000 E
T3 000000 E
T4 000000 E
T5 000000 E
T6 000000 E
T7 000000 E
T8 200712 H
T9 200801 H
T10 200802 H
T11 200803 A
T12 000000 E

and i have declared the cursor like this
EXEC SQL DECLARE CNTL_TABLE_CSR2 CURSOR WITH HOLD FOR
SELECT TBE_NME,
TBE_DTE,
FLAG
FROM ARW_CNTL_TABLE
WHERE CNTL_FLAG = 'H'
FOR UPDATE OF FLAG
END-EXEC.

i want to select only one record which is flag equal is E,if no row is having the flag E then select the older date of flag H from the table.How to select that perticular row?
Back to top
View user's profile Send private message
babu_hi

New User


Joined: 11 Apr 2006
Posts: 93

PostPosted: Thu Apr 10, 2008 12:10 pm
Reply with quote

sorry friends i have written wrong in the above post,i have declared cursor like this,

EXEC SQL DECLARE CNTL_TABLE_CSR2 CURSOR WITH HOLD FOR
SELECT TBE_NME,
TBE_DTE,
FLAG
FROM ARW_CNTL_TABLE
WHERE FLAG = 'H' or FLAG ='E'
FOR UPDATE OF FLAG
END-EXEC.
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Thu Apr 10, 2008 1:22 pm
Reply with quote

You can do a order by on FLAG ascending & TBE_DTE descending and then you can do a sequential processing till you meet your criteria.
Back to top
View user's profile Send private message
vasanthkumarhb

Active User


Joined: 06 Sep 2007
Posts: 275
Location: Bang,iflex

PostPosted: Thu Apr 10, 2008 1:27 pm
Reply with quote

Hi,
The DECLARE cursor-name statement identifies the cursor, the columns
selected, and the search criteria.
a) The column-names in the SELECT clause (TBE_NME, TBE_DTE, FLAG)
are those of the table.
b) Note that if the WHERE clause is missing we are reading the whole table.
c). We set the Cobol data-name FLAG = 'H' or FLAG ='E' to the correct value since it is used as the search criteria.

that u have declared as below

Code:
EXEC SQL DECLARE CNTL_TABLE_CSR2 CURSOR WITH HOLD FOR
SELECT TBE_NME,
TBE_DTE,
FLAG
FROM ARW_CNTL_TABLE
WHERE FLAG = 'H' or FLAG ='E'
FOR UPDATE OF FLAG
END-EXEC.


For fetching the record means, only one record dont keep FETCH SQL statement in loop such as until SQLCODE = 100 or some other means

do it in a one shot and move it to cobol variables and display it in terminal

For Updation
Code:
EXEC SQL
UPDATE CNTL_TABLE_CSR2
SET FLAG= . . . . .
WHERE CURRENT OF CURSOR-names
END-EXEC.

For fetching

Code:
EXEC SQL
FETCH cursor-name
INTO:TBE_NME:TBE_DTE:FLAG
END-EXEC.
IF SQLCODE TO EQUAL TO ZERO


the very first record that satisfies the condition will be displayed.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Thu Apr 10, 2008 2:08 pm
Reply with quote

Try:
Code:
WHERE
   (FLAG ='E')
OR
   (FLAG = 'H' AND TBE_DTE = (SELECT MAX(TBE_DTE) FROM ARW_CNTL_TABLE WHERE FLAG = 'H') )
ORDER BY FLAG

Though that may be expensive (in DB2 cost).
With the order by, you will fetch E first if there is, and if not H will come first.
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 Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top