Both commands will suck in program source code from a copybook – typically stored in QCPYLESRC. But there is one big difference between how the compile directives work in RPGLE and SQLRPGLE programs : in RPG ILE programs the /copy directive is expanded by sql preprocessor, but /include does not get expanded by SQLRPGLE code.
Why doesnt /include work in CRT RPG SQL?
RPGSQL is a little more precise on its compiles thats why!
Your RPG SQL Source code and include copybooks. These copybooks can copy in other copybooks. Which can suck in yet more copybooks. These are called levels and we can specify how many levels we want to allow using a precompile option.
How can SQLRPGLE use multiple /includes?
CRTSQLRPGI has a parameter called RPGPPOPT which defines how many levels deep any copybooks will go through to suck in copybook source code.
The screen is a little confusing:
Help for this says:
Specifies if the ILE RPG compiler will be called to preprocess the source member before the SQL precompile is run. Preprocessing the SQL source member will allow some compiler directives to be handled before the SQL precompile. The preprocessed source will be placed in file QSQLPRE in QTEMP. This source will be used for the SQL precompile.
*NONE
The compiler is not called for preprocessing.*LVL1
The compiler is called for preprocessing to expand /COPY and handle the conditional compilation directives except the /INCLUDE directive.*LVL2
The compiler will be called for preprocessing to expand /COPY and /INCLUDE and handle the conditional compilation directives.
Confused?
OK, lets try to write this in a more waffley version of English- by default, creating a SQL module or program will only suck in one copybook if you use /include but will suck in all embedded copybooks if you use /copy (when I say embedded i mean if a copybook #1 also has more /copy statements to pull in more code from copybook #2 which might have even more to pull in from copybook #3 and so on).
Using COPYBOOKS in the RPG programming language
What is a Copybook?
/copy and /include copybook code is “pulled” into the source by the compiler before executing the compile, this eliminates having to duplicate the code a million times over (but I mainly use for prototypes, data structures…not calculations!).
/define is used to segregate code in copybooks (or skip code depending on usage).
/copy and /include both are used to add program source code to RPGLE and SQLRPGLE programs.
RPGLE use either /INCLUDE or /COPY SQLRPGLE use /COPY
Why? The /COPY and /INCLUDE directives have the same purpose and the same syntax, but are handled differently by the SQL precompiler. SQL will check the RPGPPOPT parameter to see how deep it can go but RPG will always load all from multiple levels. This can lead to some head scratching moments… I know it has for me.
So, if writing SQL RPG with /include then check the RPGPPOPT parameter. Or just use /copy and dont worry about it 🙂
If your program has embedded SQL then stick with /copy (if you don't want to worry about compiling with RPGPPOPT) but if it's a native RPGLE program then you can use either.
this is actually dependent on the RPG preprocessor options (RPGPPOPT parameter) on the CRTSQLRPGI command. the default is *NONE which means the pre-compiler is NOT called for preprocessing at all. The value *LVL1 expans /COPY and conditional compilation directives, and the value *LVL2 expands /COPY AND /INCLUDE (along with conditional compilation directives).