I've experienced something very weird... a program that we've used quite bit has (on 3 occasions) created rows with duplicate keys.
First off, the files/LFs did not (they do now) have the UNIQUE keyword - I've since created a LF with UNIQUE.

I have an RPG Program that creates new shipping addresses for a Customer Number. Each Customer can have multiple ship-to address "suffixes" (key is customer + suffix).
This program is called hundreds of times by another program as it's retrieving batches of web orders (i.e. each order in the batch likely has a different shipping address).

It flows as follows:

1. Chains by address to locate existing "suffix"... if not found
2. Calls a bound procedure NextSfx() which uses an (SQL Cursor) to read through the rows (ordered by field "suffix")
* It finds a suffix "number" that is "inbetween", OR
* The last one used
3. It returns the NextSfx() value to the RPG program, which writes 2 different files that use the "suffix" as part of the key

Things to note about NextSfx()

* The suffixes are 3-character, base36 values
* It uses SrtSeq=*LangIdUnq to begin at "000"
* It uses STRTOL() "String to Integer" and __LTOA() "Integer to String" to interpret the character values and increment to the next possible value

In my mind, one of two things must have occurred.

1. The logic used for incrementing failed, OR
2. The RPG program was called so quickly that the previous record was not written

I am not sure how to resolve this... Ideas??


This is the NextSfx() function:

// String to Integer - C Routine
dcl-pr strtol int(10) extproc('strtol');
nptr pointer value options(*string);
entptr pointer value;
base int(10) value;

// Integet to String - C Routine
dcl-pr ltoa int(10) extproc('__ltoa');
nuInt int(10) value;
szRtnBuffer pointer value;
nRadix int(10) value;

dcl-s number int(10);
dcl-s base36 char(32);
dcl-s i int(10);

dcl-s LastSfx char(3);
dcl-s CurrSfx char(3);
dcl-s NextSfx char(3);

dcl-s LastNum int(10) inz;
dcl-s CurrNum int(10) inz;
dcl-s NextNum int(10) inz;
NextSfx = '000';
LastSfx = '000';

// Find the last suffix used or any suffix between
// Starts at 000 (see SQL SrtSeq @ top of module)

Exec Sql
Declare adcsr Cursor For
Select adsfx#
Where adent#=:inEnt#
Order by adsfx#
For Read Only;

Exec Sql
Open adcsr;

Dou SqlStt = sqlNoRow;

Exec Sql
Fetch Next From adcsr
Into :CurrSfx;

if CurrSfx='000';

if SqlStt = SqlNoRow;
LastNum = CurrNum;

base36 = CurrSfx;
CurrNum = strtol(%trim(base36):*null:36); // Alpha to Num
if CurrNum - LastNum = 1;
LastNum = CurrNum;


Exec Sql close adcsr;

NextNum = LastNum + 1;
base36 = *blanks;
ltoa(NextNum:%addr(base36):36); // Num to Alpha

// Uppercase & Removes trailing spaces in base36 string
for i = 1 to %len(%trim(base36));
%subst(base36:i:1) = %bitor(%subst(base36:i:1):X'40');

i = 1 + %len(NextSfx) - %len(%trim(base36));
%subst(NextSfx:i) = %trim(base36);
NextSfx = '###';

return NextSfx;
[Logo]<https://www.totalbizfulfillment.com/> Greg Wilburn
Director of IT
301.895.3792 ext. 1231
301.895.3895 direct
1 Corporate Dr
Grantsville, MD 21536

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.