CL Programming
CL Programming
CL Programming
AS/400-CLP 1
CL commands
CL command is a statement that requests the
system to perform a function.
Function is performed by a program that is run
when the command is entered.
AS/400 has more than thousand commands that
perform the function.
AS/400-CLP 2
CL Program Structure
Description Commands
Start PGM(Optional)
Declare Statement DCLF , DCL
Program Level monitor MONMSG
message
IBM Commands and/or RCVF . . .
user commands MONMSG
(command-level)
End ENDPGM
AS/400-CLP 3
CL Command Syntax
• Keyword form :
DSPLIB LIB(TESTLIB ) OUTPUT(*PRINT )
DSPLIB OUTPUT(*PRINT ) LIB(TESTLIB)
• Positional form :
DSPLIB TESTLIB *PRINT
• Combination form :
DSPLIB TESTLIB OUTPUT(*PRINT )
AS/400-CLP 4
Declare Commands
• Declare CL variable : DCL
Syntax : DCL VAR( ) TYPE( ) LEN( ) VALUE( )
• Examples :
( 1 ) DCL &VAR1 *CHAR 2 ‘AA’
(2) DCL VAR( &VAR2 ) TYPE( *DEC ) LEN( 4 2 ) VALUE( 12.78 )
AS/400-CLP 5
Declare Commands Contd...
AS/400-CLP 6
Logic Control Commands
• IF ... THEN ... ELSE
• DO … ENDDO
• GOTO
AS/400-CLP 7
Logic Control Commands contd..
• IF ... THEN … ELSE…
Example : IF COND( &OPTION *EQ ‘1’ )
AS/400-CLP 8
Logic Control Commands contd..
• DO ... ENDDO
Example : IF COND( &OPTION *EQ ‘1’ )
THEN(CALL PGM1 )
ELSE CMD( DO)
CALL PGM2
CALL PGM3
ENDDO
CALL PGM( PGM5)
• Program PGM2 & PGM3 is executed when option is
other than 1
• Program PGM5 is always executed
AS/400-CLP 9
SELECT WHEN (condition-1) THEN(command-1) . . . WHEN (condition-n) THEN(command-n) OTHERWISE command-x ENDSELECT
AS/400-CLP 10
Logic Control Commands contd…
• GOTO
Example : .
DCLF FILE( FILE1 )
.
.
LOOP : RCVF
.
GOTO CMDLBL( LOOP )
.
AS/400-CLP 11
Logic Control Commands contd…
• DOWHILE
Example : .
DOWHILE (&LGL)
.
.
.
IF (&INT *EQ 2) (CHGVAR &LGL '0')
IF (&INT *EQ 3) (ITERATE)
IF (&INT *EQ 3) (LEAVE)
ENDDO
AS/400-CLP 12
Logic Control Commands contd…
• DOUNTIL
Example : .
DOUNTIL (&LGL)
.
.
.
IF (&INT *EQ 2) (CHGVAR &LGL '0')
IF (&INT *EQ 3) (ITERATE)
IF (&INT *EQ 3) (LEAVE)
ENDDO
AS/400-CLP 13
Logic Control Commands contd…
• DOFOR
Example : .
CHGVAR &INT2 0
DOFOR VAR(&INT) FROM(2) TO(4) BY(1)
.
.
.
CHGVAR &INT2 (&INT2 + &INT)
ENDDO
AS/400-CLP 14
Logic Control Commands contd…
• Relational Operators
Which can be specified by symbols (=, >, <, >=, <=, =, >, <)
or their reserved values (*EQ, *GT, *LT, *GE, *LE, *NE,
*NG, *NL).
• Logical Operators
*AND and *OR (as reserved values), and & and | (as
symbols). *NOT (or ¬ )
AS/400-CLP 15
CL processing commands
AS/400-CLP 16
Built-In Functions
• %SUBSTRING or %SST
Syntax : %SST( <character-variable > < starting position > <length> )
Examples :
• CHGVAR %SST( &NAME 2 3 ) ‘999’
AS/400-CLP 17
Concatenation
Used to combine two char variables or values
• *CAT : concatenate without editing
Welbbb *CAT comebbb = Welbbbcomebbb
• *TCAT : All trailing blanks of first string is truncated .
Welbbb *TCAT comebbb = Welcomebbb
• *BCAT : All trailing blanks of first string is truncated and
one blank is inserted.
Welbbb *BCAT comebbb = Welbcomebbb
Example :
CHGVAR VAR(&NAME) VALUE ( &VAR1 *BCAT &VAR2 )
AS/400-CLP 18
Built-In Functions
• %CHECK
The check built-in function (%CHECK) returns
the first position of a base string that contains a
character that does not appear in the comparator
string. If all of the characters in the base string
also appear in the comparator string, the function
returns 0.
%CHECK(comparator-string base-string [starting-
position])
AS/400-CLP 19
Built-In Functions
• %CHECKR
Similar to %CHECK, but checks from right to left.
AS/400-CLP 20
Built-In Functions
• %SCAN
The scan built-in function (%SCAN) returns the
first position of a search argument in the source
string, or 0 if it was not found.
AS/400-CLP 21
Built-In Functions
• %TRIM - Leading and trailing blanks removed
%TRIM(character-variable-name [characters-to-trim])
AS/400-CLP 22
Date Conversion
• CVTDAT
CVTDAT DATE('12-24-88') TOVAR(&DATE)
TOFMT(*DMY)
Keyword Description Choices Notes
DATE Date to be converted Character value Required, Positional 1
TOVAR CL var for converted date Character value Required, Positional 2
FROMFMT From date format *SYSVAL, *MDY, *DMY, *YMD, Optional, Positional 3
*JUL, *JOB, *MDYY, *DMYY,
*YYMD, *CYMD, *ISO, *USA,
*EUR, *JIS, *LONGJUL
AS/400-CLP 23
Program control commands
• CALL : Used to execute a program .
AS/400-CLP 24
Program control commands contd..
• CALL
Example :
CALL PGM( PGMA ) PARM( &PARM1 100 ‘1234’ )
AS/400-CLP 25
Program control commands contd..
• TFRCTL
Example : TFRCTL PGM( PGM1 ) PARM ( P1 , P2 …)
PGMA PGMB PGMC
. .
CALL PGMB TFRCTL PGMC
. .
. . RETURN
AS/400-CLP 26
Send messages from a CL
• SNDPGMMSG - Send Program Message
Sends a message to a named message queue or to a call
message queue.
SNDPGMMSG MSG(&MSG)
AS/400-CLP 27
Handling Exceptions in CL
• MONMSG :
Syntax : MONMSG( <message-id> ) EXEC( <CL-command> )
AS/400-CLP 28
Handling Exceptions in CL
• Program-level MONMSG :
Example :
PGM
DCLF . . .
MONMSG (CPF0000) EXEC ( GOTO LAST )
CALL PROGA
CALL PROGB
RCVF
.
.
LAST : SNDPGMMSG (‘Error occurred , refer JOBLOG ‘) . . .
ENDPGM
AS/400-CLP 29
Handling Exceptions in CL
• Command- level MONMSG :
Example : Trapping divide by 0 error
PGM
DCL . . .
.
CHGVAR VAR( &A ) VALUE( &A/&B )
MONMSG MSGID( MCH1211 ) EXEC( CHGVAR &A 1 )
CALL PROGA
MONMSG MSGID( CPF0001 ) EXEC( GOTO LAST )
.
.
LAST : SNDPGMMSG ( ‘ CALL command failed ‘) . . .
ENDPGM
AS/400-CLP 30
Exercise
1. Accept character string as parameter and reverse the
string and send the result to message queue.
2. Accept character string and remove the spaces between
characters in input string and send output as message.
3. Accept character date into program and validate the date
and give message if its invalid.
AS/400-CLP 31
Major groups
AS/400 has a built-in menu called MAJOR that
organizes all the AS/400 commands.
Say GO MAJOR in the command line to access
the above.
AS/400-CLP 32
Major
Select Command by Name SLTCMD
Verb Commands VERB
Subject Commands SUBJECT
Object Management Commands CMDOBJMGT
File Commands CMDFILE
Work Management Command CMDWRKMGT
Data Management Command CMDDTAMGT
Security Commands CMDSEC
AS/400-CLP 33
Major
Print Commands CMDPRT
Spooling Commands CMDSPL
System Control Commands CMDSYSCTL
Program Commands CMDPGM
Office Commands CMDOFC
Database Commands CMDDB
Communications Management
Commands CMDCMNMGT
AS/400-CLP 34
Major
Distribution Service Commands CMDDSTSRV
Configuration Commands CMDCFG
Problem Management
Commands CMDPRBMGT
Message Handling Commands CMDMSGHDL
Performance Commands CMDPFR
System/36 Commands CMDS36
System/38 Commands CMDS38
AS/400-CLP 35
File Commands
FILE TYPE CREATE CLEARDELETE ADDING
MEMBERS
Source Physical CRTSRCPF CLRPFM DLTF ADDPFM
File
Data Physical File CRTPF CLRPFM DLTF ADDPFM
AS/400-CLP 36
File Commands
DSPFD - Display File Description
DSPFFD - Display File Field Description
DSPDBR - Display Data Base Relations
CPYF - Copy File
RMVM - Remove Member
DSPOBJD - Display Object Description
CHGPF - Used to change attributes of PF
CHGLF - Used to change attributes of LF
AS/400-CLP 37
Program Object Command
CRTRPGPGM- Create RPG program
CRTCLPGM- Create CL Program
CRTBNDRPG
- Create RPGLE program
DLTPGM - Delete Program
DSPPGMREF- Displays all object referred in
the program
RTVCLSRC - Retrieve CL source
CRTCBLPGM - Create Cobol program
AS/400-CLP 38
Retrieve and Conversion
Commands
RTVMBRD - Retrieve member description
RTVJOBA - Retrieve Job attributes
RTVSYSVAL- Retrieve System Value
RTVOBJD - Retrieve Object Description
CVTDAT - Changes the format of Date
CHGVAR - Changes the value of Variable
AS/400-CLP 39
Submitting job
Job
Interactive job
Batch job
SBMJOB - The submit job command is used to
submit a job to a job queue (e.g
QBATCH) for batch
processing.
DLYJOB – To delay job execution.
AS/400-CLP 40
Job Commands
WRKJOB - Work with Job
WRKSBMJOB - Work with Submitted Jobs
WRKUSRJOB - Work with User Jobs
AS/400-CLP 41
Library Commands
DSPLIB - Display library
WRKLIB - Work with library
ADDLIBLE - Add library list entry
CHGLIBL - Change library list
EDTLIBL - Edit library list
RMVLIBLE - Remove library list
DSPLIBL - Display library list
AS/400-CLP 42
General Object Command
WRKOBJ - Work with Object
CHKOBJ - Checks for existence of Object
and necessary authority
CRTDUPOBJ - Create Duplicate Object
AS/400-CLP 43
Spool file Commands
WRKSPLF - Work with spool file
CPYSPLF - Used to convert spool file to
database file
DSPSPLF - Display spool file
DLTSPLF - Delete spool file
AS/400-CLP 44
STRSDA
1. Design screens
2. Design menus
3. Test display files
AS/400-CLP 45
Screen Design using SDA
• Adding constant : Write text inside the
qutoations.
• Adding field: Enter + just before where you
want to add field. Ex: +9(3) or +B(10)
3 Numeric input field
6 Numeric output field
9 Numeric both field (input and output)
I or i Alphabetic input field
O or o Alphabetic output field
B or b Alphabetic both field (input and output)|
AS/400-CLP 46
Screen Design using SDA
• Change field attribute : Put ? before field
and press Enter.
• To copy a field:
1. Type a minus sign (−) in the attribute position of the
field to be moved.
2. Type two equals signs (==) in the attribute position
of the receiving location.
3. Press Enter.
• Deleting Fields
Type D (or d) in the attribute position of the field
AS/400-CLP 47
Screen Design using SDA
• Moving Fields
To move fields, type the minus sign (−) in the attribute
position of the field and then equals sign (=) where you
want the field to appear.
• Specifying Display Attributes
B or b Blink All
S or s Column separators Design Image
H or h Highlight All
R or r Reverse image All
U or u Underline All
N or n Nondisplay Design Image
AS/400-CLP 48
Screen Design using SDA
• Removing Display Attributes
Type a minus sign (−) in the attribute position of the
field and type the display attribute to be deleted over
the first character. If you type −A or −a, all attributes
for the field are deleted.
• Specifying Color
CB Blue
CG Green
CP Pink
CR Red
CT Turquoise
AS/400-CLP 49
Screen Design using SDA
• Removing Color from a Field
Type a minus sign (−) in the attribute position of the
field and type the color code over the first two
characters of the field. For example, to remove a blue
color from a field, type −CB. If you type −CA, all
colors specified for that field are removed.
• Centering Fields
To center a field in the same row, type ac in the
attribute position of the field
AS/400-CLP 50
File Commands
RCVF - Reads record from the
database or display file.
SNDF - Writes a record to display file.
SNDRCVF - Display the record of display file till
it receives any entry
from the user.
AS/400-CLP 51
User Defined Command
Command require source to define command parameters.
AS/400-CLP 52
User Defined Command
CRTCMD: To create user defined command using
command source and link the existing program.
AS/400-CLP 53
Menu Design using SDA
• We can design Menu and assign
command to each menu option using
STRSDA.
• To execute menu ‘GO menu-name’
AS/400-CLP 54
Exercise (Day 2)
Design a screen with student number as input and
Student name as output, accept the student number
and output student name by searching the student
file.
Create file in library QTEMP copying first 5
records of a file.
Create a menu with options 1. Add, 2. Subtract , 3.
Multiply and 4. Division. Once user takes
particular option, screen should take two number
and display the result in third field as per the
operation selected in menu.
AS/400-CLP 55
Override Command
Used to qualify the library where file is located
Used to override the file specified in program to
use the file specified in TOFILE parameter and
particular member in file.
Override certain attributes of file being processed
e.g SHARE(*NO) can be overridden to
SHARE(*YES)
AS/400-CLP 56
Override Command
OVRDBF - Override database file
OVRPRTF - Override printer file
OVRDSPF - Override display file
DLTOVR - Delete override
DSPOVR - Display override
POSDBF - Position database file
AS/400-CLP 57
Open Query file
Allows to retrieve data from database to a high-level
language program
Selects records that meet certain criteria
Create new fields and summarize data
Used to create alternate key and sort records
Can used for key processing, Group processing and
Random access
Supports relational, logical, arithmetic and string
operations on fields
AS/400-CLP 58
Open Query file
OPNQRYF - Open query file
CPYFRMQRYF - Copy from query file
Create, Replace, Add,Selection of records
CLOF - Close file
AS/400-CLP 59
Data Areas
Area to pass information within Job
Can use constant field for several jobs
To provide field that is easily and
frequently changed within a job
Can be locked to single user preventing
other users from processing at the same
time
AS/400-CLP 60
Types of Data Areas
General Data Area ( *DTAARA )
Local Data Area ( *LDA )
Group Data Area ( *GDA )
Program Initialisation Parameter Data Area
( *PDA )
AS/400-CLP 61
CL Data Area Operation
CRTDTAARA - Create Data Area
DSPDTAARA - Display Data Area
CHGDTAARA - Change Data Area
RTVDTAARA - Retrieve Data Area
DLTDTAARA - Delete Data Area
AS/400-CLP 62
Create Data Area (CRTDTAARA)
Bottom
F3=Exit F4=Prompt F5=Refresh F10=Additional parameters
F12=Cancel F13=How to use this display F24=More keys
AS/400-CLP 63
Command Syntax
CRTDTAARA DTAARA(Library/Name)
TYPE(Type of data)
LEN(Length [Decimal Position])
VALUE(Initial Value)
AS/400-CLP 64
Display Data Area (DSPDTAARA)
Bottom
F3=Exit F4=Prompt F5=Refresh
AS/400-CLP
F12=Cancel 65
F13=How to use this display F24=More keys
Display Data Area
System: S7829712
Data area . . . . . . . : TEST
Library . . . . . . .: MGSAWRK
Type . . . . . . . . . . : *DEC
Length . . . . . . . . .: 52
Text . . . . . . . . . . .: Test Data Area
Value . . . . . . . . . . : 100.25
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys AS/400-CLP 67
Retrieve Data Area (RTVDTAARA)
Bottom
F3=Exit F4=Prompt F5=Refresh AS/400-CLP
F12=Cancel F13=How to use this display
68
F24=More keys
Delete Data Area (DLTDTAARA)
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys AS/400-CLP 69
Columns . . . : 1 71 Edit MGSAWRK/NMSRC
SEU==> EXCLP
FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
*************** Beginning of data *************************************
0001.00 PGM
0002.00 DCL VAR(&DATA) TYPE(*CHAR) LEN(25)
0003.00 CRTDTAARA DTAARA(MGSAWRK/MYDATA) TYPE(*CHAR) LEN(25) +
0004.00 VALUE('THIS IS MY DATA.') TEXT('TEST DATA AREA’)
0005.00 RTVDTAARA DTAARA(MGSAWRK/MYDATA) RTNVAR(&DATA)
0006.00 SNDPGMMSG MSG(&DATA)
0007.00 DLTDTAARA DTAARA(MGSAWRK/MYDATA)
0008.00 ENDPGM
****************** End of data ****************************************
AS/400-CLP 72
MESSAGE FILE COMMANDS
• CRTMSGF • ADDMSGD
• CHGMSGF • CHGMSGD
• DLTMSGF • RMVMSGD
• WRKMSGF • WRKMSGD
AS/400-CLP 73
Create Message File (CRTMSGF)
Additional Parameters
File size:
Initial storage size . . . . 10 Kilobytes
Increment storage size . . . 2 Kilobytes
Maximum increments . . . . . *NOMAX Number, *NOMAX
Authority . . . . . . . . . . *LIBCRTAUT Name,*LIBCRTAUT,*CHANGE
Coded character set ID . . . . *HEX *HEX, *MSGD, *JOB...
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys
AS/400-CLP 74
Change Message File (CHGMSGF)
Additional Parameters
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys
AS/400-CLP 75
Delete Message File (DLTMSGF)
AS/400-CLP Bottom 76
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
WRKMSGF
Work with Message Files (WRKMSGF)
Message
Opt File Library Text
QALRMSG QSYS
QBASMSG QSYS
QBMGMSGF QSYS
QCBLMSGE QSYS COBOL RUN TIME MESSAGE FILE
QCEEMSG QSYS
QCPFMSG QSYS
QCWXMSG QSYS CUBE-3 MESSAGE FILE
QC2MSGF QSYS ILE C RUN TIME MESSAGE FILE
QDFUMSG QSYS
More...
Parameters for options 1, 2, 5, 12 and 13 or command
===>
F3=Exit F4=Prompt F5=Refresh F9=Retrieve F11=Display names only
F12=Cancel F16=Repeat position to F17=Position to F24=More keys
AS/400-CLP 77
Work with Message Files (WRKMSGF)
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
AS/400-CLP 78
F24=More keys
Work with Message Files
Message
Opt File Library Text
QALRMSG QSYS
QBASMSG QSYS
QBMGMSGF QSYS
QCBLMSGE QSYS COBOL RUN TIME MESSAGE FILE
QCEEMSG QSYS
QCPFMSG QSYS
QCWXMSG QSYS CUBE-3 MESSAGE FILE
QC2MSGF QSYS ILE C RUN TIME MESSAGE FILE
QDFUMSG QSYS
More...
Parameters for options 1, 2, 5, 12 and 13 or command
===>
F3=Exit F4=Prompt F5=Refresh F9=Retrieve F11=Display names only
F12=Cancel F16=Repeat position to F17=Position to F24=More keys
AS/400-CLP 79
ADDMSGD
Add Message Description (ADDMSGD)
...
Severity code . . . . . . . . . 00 0-99
More...
F3=Exit F4=Prompt F5=Refresh F10=Additional parameters F12=Cancel
F13=How to use this display F24=More keys
AS/400-CLP 80
ADDMSGD (Contd…)
Add Message Description (ADDMSGD)
Replacement to-value . . . . .
+ for more values
AS/400-CLP 81
More...
ADDMSGD (Contd…)
Add Message Description (ADDMSGD)
Bottom
F3=Exit F4=Prompt F5=Refresh F10=Additional parameters F12=Cancel
F13=How to use this display F24=More keys
AS/400-CLP 82
RMVMSGD
Remove Message Description (RMVMSGD)
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys AS/400-CLP 83
DSPMSGD
Display Message Description (DSPMSGD)
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel F13=How to use this display
F24=More keys AS/400-CLP 84
DSPMSGD(Contd…)
Select Message Details to Display
System: S7829712
Message ID . . . . . . . : CAE0002
Message file . . . . . . : QCPFMSG
Library . . . . . . . : QSYS
Message text . . . . . . : New level of CSP/AE required for application
Selection
F3=Exit F12=Cancel
AS/400-CLP 85
Work with Message Descriptions (WRKMSGD)
Bottom
F3=Exit F4=Prompt F5=Refresh F12=Cancel
AS/400-CLP F13=How to use this display
86
F24=More keys
Work with Message Descriptions
System: S7829712
Message file: QCPFMSG Library: QSYS
Position to . . . . . . . Message ID
AS/400-CLP 88