4gl update and report
Posted in 1995
1. I have 4.1 SQL, Tools, 4gl and 5.0 on-line engine.
2. I am processing two tables in two different databases in the same on-line engine.
3. I am attempting to write a 4gl program to update table A with the
changes indicated in table B. A single key field joins the two tables.
Table A could have duplicate keys and Table B does not have duplicate keys.
Both tables could have rows not contained in the other but all I want to do
is update rows in table A where the key in table A matches the key in table
B.
4. I used the DATABASE statement to set the database of the table that I am
going to update and used the colon to reference the table in the other
database.
5. I declared a cursor;
DECLARE cursor FOR SELECT II:B.*, A.*, A.ROWID
INTO tempB.*, tempA.*, arowid
FROM II:B, A
WHERE A.key = II:B.key
ORDER BY A.name
I couldn't use the 'FOR UPDATE' clause since it only works with a simple
select.
6. I have to use OPEN, FETCH, CLOSE since I have duplicate keys. My loop
then looks like the following:
OPEN cursor WHILE STATUS = 0
flag = 0
FETCH cursor
IF STATUS <> 0 THEN
EXIT WHILE
END IF
IF tempA.fld5 <> tempB.fld6 THEN
OUTPUT TO REPORT rpt tempA.fld5, tempB.fld6
LET tempA.fld5 = tempB.fld6
LET flag = flag + 1
END IF
IF tempA.fld7 <> tempB.fld3 THEN
OUTPUT TO REPORT rpt tempA.fld7, tempB.fld3
LET tempA.fld7 = tempB.fld3
LET flag = flag + 1
END IF
IF flag <> 0 THEN
UPDATE A SET A.* = tempA.* WHERE ROWID = arowid
END IFDISPLAY tempA.key, flag, tempA.fld5, tempA.fld7
DISPLAY tempB.key, tempB.fld6, tempB.fld3
END WHILE
(The DISPLAY statements are for debugging purposes)
7. The report output, the screen DISPLAY data and the actual table rows
accessed by Perform screens should all reflect the same data however they
don't. Sometimes the DISPLAY will show that the data between the two did in
fact change (but the flag = 0) as does the actual rows but the report says
that there were no changes. When this happens the key fields in the DISPLAY
are equal (say the key is 10 for example) but the fld5 and fld7 data are for
the previous row (key 9). If the next line on the report is wrong then the
data DISPLAYed for fld5 and fld7 is for key 9 also but the key displayed is
11. At least once the flag DISPLAYED 0 but the report correctly showed that
the data changed.
I haven't actually enabled the UPDATE statement yet except for 4 specific
keys and those rows updated correctly.
Everything that I have read and tried has not fixed the problems. Dose any
one have any ideas?
DISPLAY reported
key flag fld5 fld7
9 2 22 32 9 22 32
9 22 32
10 0 22 32 no change
10 55 64
11 0 22 32 no change
11 16 9
12 0 62 35 12 62 35
~0c12 62 35
In reality there were correct transactions in between these etc.
~