I have a table in an Informix database, let's say it is called store, that looks like:
| serial_num | store_seq |
|---|---|
| 381-15 | 82600 |
| 381-15 | 82610 |
| 381-15 | 82640 |
| 381-15 | 82650 |
| 381-15 | 82680 |
| 381-114-1 | 82690 |
I have to update serial_num so that numbers are updated incrementally like this:
| serial_num | store_seq |
|---|---|
| 381-15-1 | 82600 |
| 381-15-2 | 82610 |
| 381-15-3 | 82640 |
| 381-15-4 | 82650 |
| 381-15-5 | 82680 |
| 381-114-1 | 82690 |
Transcribing a comment into the question:
I tried creating a procedure:
CREATE PROCEDURE update_store()
DEFINE counter INTEGER;
DEFINE temp_serial_num CHAR(20);
FOREACH cur1 FOR
SELECT serial_num INTO temp_serial_num FROM store WHERE serial_num = '381-15'
LET counter = 0;
WHILE counter < (SELECT COUNT(*) FROM store WHERE serial_num = '381-15')
LET counter = counter + 1;
UPDATE store
SET serial_num = temp_serial_num || '-' || counter::CHAR
WHERE CURRENT OF cur1;
END WHILE;
END FOREACH;
END PROCEDURE;
The result was 381-15-5 in all 381-15 numbers.
NB: The serial_num column is a CHAR(16) column; store_seq is an INTEGER column.
After fixing up some name mismatches between the code in the comment and the schema of the table, I find your stored procedure doesn't change the data. However, that's in part because you don't use TRIM to remove the trailing blanks from
serial_numbefore appending the new counter. The TRIM would not be necessary if theserial_numcolumn was a VARCHAR instead of a CHAR, but it would do no harm beyond slowing things down a little.I added an Informix SERIAL column
rec_numto the table:A modified procedure takes the
serial_numto fix as a parameter:When run, I get the output:
This changes the data in the table, but because no ordering was applied, the rows are not updated in the order of the
store_seqvalue, which it appears is the desired result.A rewritten procedure deals with the ordering problem:
The
new_serial_numvariable (and theTRACE ON;statement) helped identify the missing TRIM problem.This produces the output:
You can't apply a
WHERE CURRENT OF cur1clause to an UPDATE when the cursor has an ORDER BY clause on it. That's why the processing identifies the row to be updated byserial_numandstore_req.