01 - Programmazione01a - RPG

SQLCODE: USIAMOLO BENE

Last Updated on 3 Marzo 2020 by Roberto De Pedrini

Non ho l’abitudine di curiosare nel codice scritto dai colleghi, ma qualche volta mi tocca. Spesso noto un errore nell’uso di SQLCODE: è importante chiarire che il test “SQLCODE <> 0” è sbagliato. Rileggiamo un attimo quello che c’è scritto nel manuale “SQL reference”:

The database manager sets SQLCODE after each SQL statement (…) is executed. SQLCODE is set as follows:

  • If SQLCODE = 0 and SQLWARN0 is blank, execution was successful.
  • If SQLCODE = 100, no data was found. For example, a FETCH statement returned no data, because the cursor was positioned after the last row of the result table.
  • If SQLCODE > 0 and not = 100, execution was successful with a warning.
  • If SQLCODE = 0 and SQLWARN0 = ‘W’, execution was successful with a warning.
  • If SQLCODE < 0, execution was not successful.

Dalle parti che ho evidenziato risulta chiaro che il test “SQLCODE <> 0” è sbagliato, perché solo un SQLCODE negativo segnala una esecuzione fallita, mentre un SQLCODE positivo segnala una esecuzione con successo. Quindi il controllo giusto (p.e. dopo una FETCH) è:

if sqlCode < 0; // Esecuzione fallita.
  dump(a);
  leave;
elseif sqlCode = 100; // No data.
  leave;
else; // FETCH riuscita.
  // Elaborazione della riga.
endif;

Related Posts
DB2 for i SQL – Stringhe – POSSTR-LOCATE-LOCATE_IN_STRING (IT)

Introduzione Spesso, nelle nostre applicazioni, abbiamo la necessità di lavorare con le stringhe di testo e l'SQL del DB2 può Read more

DB2 for i & SQL – FAQ & Howto (Part. 1) (IT)

Database DB2 e SQL ... forse lo strumento più potente e completo che abbiamo sulla piattaforma IBM i: ecco una Read more

Annuncio IBM i 7.4

Arriva direttamente con l'uovo di Pasqua questo annuncio IBM per le novità della versione IBM i 7.4, versione iNext secondo Read more

Generated Always Columns – Approfondimenti (IT)

Introduzione "Generated Always Column": sono colonne, campi, di una tabella il cui contenuto è controllato direttamente dal sistema ... e Read more

About author

Dal 1989 sviluppo applicazioni gestionali per IBM i.

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *