RPG – EXEC SQL

EXEC SQL is used to put an SQL script embedded in your RPG code and have it executed as part of the program logic.

You need to prefix your RPG variables with a : in order to access them within the SQL statement. These are also called Host varibles.
It has to do with how the compiler works. This is because the SQL pre-compiler runs before the RPG compiler.

The SQL statement ends with a semi-colon ( ; ).

Example