Mainframe programming — NATURAL/ADABAS tutorial. Part 2 — how to work with database
Basics of Mainframe programming for beginners.

This is my third article about developing on Mainframe in NATURAL language and the one where we start to interact with a database. In fact NATURAL is the most powerful when it needs to use database, so let’s dive deep in! If you want to read the previous ones, which are gentle introduction to the world of Mainframes, take a look at the links below.
Here are all articles I have wrote about Mainframes so far:
- Intro — a few words of introduction.
- Part 1 — setup process and “Hello World” code
- Part 2 — how to work with database (the one you are reading now)
- Part 3 — error Handling and Debugging Techniques
I have a database — how can I access it?
Usually as a developer you won’t be creating a database itself — you will have to work with data from the database that already exists. Another typical thing, is that you normally don’t need access to all fields the existing database provides. You may want to only use a few particular fields, so you don’t want to bother reading all of them. Of course this is something you can easily do in NATURAL. In simple words you need to tell the program what database you want to read, then created so called a view on it, and then specify which fields you will be using. Sounds pretty straightforward right? You do all this while defining your data — we know how to do it already. So here’s an example:
DEFINE DATA
LOCAL
1 STUDENTS-VIEW VIEW OF STUDENTS
2 FULL-NAME
3 NAME (A20)
2 MAJOR (A10)
2 START-YEAR (N4)
END-DEFINE
In this really simple example above we can see in the first line the name of your view STUDENTS-VIEW
and the name of the database STUDENTS
. Of course you can name your view whatever you want, but this is a common practice to construct it this way. However there are times, when for readability reasons you may want to have two views on the same database in the same program. Then of course you can easily do this by naming your views differently. Then in the next lines we have fields we would like to take from this database, we can see that we have 2nd and 3rd level here. What it means and what are those values in bracet, like (A20)
, you can read in the previous articles in the part about defining data. So you can see that we will want to know what is the student name, what year he/she started studies and what is the major. Now you can start writing your own program using those data.
How to read the database and display fields
Reading a database and then using the data from it is really easy in NATURAL. You literally need to tell the program to READ
and then DISPLAY
. Here is the example code fot this:
READ STUDENTS-VIEW BY NAME
DISPLAY NAME 2X MAJOR 2X START-YEAR
END-READ
And yup, that’s it! How simple is that. The instruction BY NAME
tells the program to sort your results in alphabetical order by name. Those 2X
marks indicates, that there should be 2 spaces displayed between columns. And while we talk about columns — what is cool about DISPLAY
instruction is that it automatically adds column headers at the top of the results. Those headers are obviously taken from database (DDM). Also the line separator is added automatically. Please take note, that if you have many results you may have more that one page. To go to the next pages you simply need to click enter. So after running this program now we would see something like that:
Page 1 18-09-03 11:20:29NAME MAJOR START YEAR
-------------------- ---------- ----------
ANTONOV CHEMISTRY 2011
BERKELEY MUSIC 2013
DUCHAMP ART 2017
KELSEY ENGLISH 2015
MODRIC PSYCHOLOGY 2018
Isn’t that awesome? Sure it is. But what if you would like to filter your results somehow, i.e. you only want to find students with art as a major? There is obviously an easy way to do that too. Before you can do that you need to define a new variable in your LOCAL
.
1 #MAJOR-WANTED (A10) INIT <"ART">
As this is a user define variable we used # at the first place in it — this is not mandatory, but this is useful and well known convention. INIT
is a keyword which allows us to define default value for our variable. So now we are ready to search our artistic gifted students. We can add this after our READ
statement just like that:
READ STUDENTS-VIEW BY NAME WITH #MAJOR-WANTED
Please note that we have used WITH
keyword to find students with searched major. We were quite exact in our searched. The other option would be to define two variables for students’ names, like #START-NAME
and #END-NAME
, and then use it like this
STARTING FROM #NAME-START
ENDING AT #NAME-END
This is how the whole code looks like
That was relatively easy right? NATURAL is really created to work with databases so as you can see, working on it is not that complicated. Of course there is a lot of options and the bigger the program gets the more complicated it may be, but some useful and working simple program can look just like what we have created in this article.
DEFINE DATA
LOCAL
1 #MAJOR-WANTED (A10) INIT <"ART">
1 STUDENTS-VIEW VIEW OF STUDENTS
2 FULL-NAME
3 NAME (A20)
2 MAJOR (A10)
2 START-YEAR (N4)
END-DEFINE
READ STUDENTS-VIEW BY NAME WITH #MAJOR-WANTED
DISPLAY NAME 2X MAJOR 2X START-YEAR
END-READ
END
This article is part of a series I write about NATURAL programming. Please note, that I am currently working as a junior mainframe developer, so I learn the secrets of the language myself. If you see any errors in my articles, please let me know! If you like the article and you believe there should be more articles about Mainframes please clap a bit, leave a comment or share this link wherever you want.

✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.