View previous topic :: View next topic
|
Author |
Message |
babu_hi
New User
Joined: 11 Apr 2006 Posts: 93
|
|
|
|
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 |
|
|
babu_hi
New User
Joined: 11 Apr 2006 Posts: 93
|
|
|
|
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 |
|
|
Srihari Gonugunta
Active User
Joined: 14 Sep 2007 Posts: 295 Location: Singapore
|
|
|
|
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 |
|
|
vasanthkumarhb
Active User
Joined: 06 Sep 2007 Posts: 275 Location: Bang,iflex
|
|
|
|
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 |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
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 |
|
|
|