4GL - confused about update cursors, transactions, "with hold" and best way to carry out a specific task
Posted in 1998
OK folks, I think this is a straightforward 4GL question but I'm feeling the need to tap into the Collective Mind... I have a table with about 300,000 rows and it looks like:- +-------+-------+-------+-------+-------+----------+ | data1 | data2 | data3 | data4 | data5 | result | +-------+-------+-------+-------+-------+----------+ | | | | | | | | Y | 460 | 32467 | 0004 | U | 12248865 | | Y | 100 | 49698 | 0010 | R | 59954984 | | ... | | +----------------- ------------------+ \\ / \\/ unique index on (data1,data2,...data5) The value of "result" is to be computed from the data in the five columns, and lookups in other tables, & some reasonably convoluted logic. How best to have a 4GL program do this, to set the value of "result"? How about this:- | DECLARE c1 | CURSOR FOR | SELECT data1, data2, data3, data4, data5 FROM my_table | FOR UPDATE OF result | | foreach c1 into p_data1, p_data2, p_data3, p_data4, p_data5 | | [compute p_result based on these values and DB lookups] | | UPDATE my_table SET result = p_result WHERE CURRENT OF c1 | | end foreach I'm a bit confused over the conditions about what can and cannot happen within a transaction, ought my cursor be "WITH HOLD", and so on. My DB is non-MODE ANSI but has transactions. My manual suggests that "each update... must take place within a transaction". I feel that I don't really want to use transactions here - and certainly I don't want to put the entire FOREACH within a transaction, as there will be many many updates and I'll get a long transaction error. If there is a problem using an update cursor in this situation then I could always do without it by issuing my update statement along the lines of:- UPDATE my_table SET result = p_result WHERE data1 = p_data1 AND ... AND data5 = p_data5 Would that be much slower? I could always add a serial column to the table and use that as the basis for the update - or I could use rowid (but only if the table isn't fragmented, yes)? The program is clearly going to have to run for quite a long time in order to update all 300,000 rows. I'm using INFORMIX-4GL Version 6.05.UD2 & OnLine 7.2 There is probably no deep mystery here, but as I say I'm somewhat confused over the interaction of update cursors, transactions, "with hold", etc. Can anyone point me in the right direction here, or away from any hazards into which I appear to be stumbling? - Paul (not a spokesman) Happy New Year to all.