Urc SQL
Urc SQL
Contents
Course Description xv
Agenda xviii
Module 1: Introduction
Materials 1-3
SG 3605 iii
Relational Concepts and SQL Programming
Materials 2-3
Materials 3-3
iv SG 3605
Relational Concepts and SQL Programming
SG 3605
Relational Concepts and SQL Programming
Materials 4-3
Materials 5-3
vi SG 3605
Relational Concepts and SQL Programming
Materials 6-3
SG 3605 vii
Relational Concepts and SQL Programming
Materials 7-3
AS Clause 7-5
Materials 8-3
Equijoins 8-4
Self-Joins 8-10
viii SG 3605
Relational Concepts and SQL Programming
Materials 9-3
SG 3605 ix
Relational Concepts and SQL Programming
Materials 10-3
Materials 11-3
x SG 3605
Relational Concepts and SQL Programming
Materials 12-3
Materials 13-3
Views 13-4
Basic Definition 13-4
What is a View? 13-5
Purpose of Views 13-6
SG 3605 xi
Relational Concepts and SQL Programming
Materials 14-3
Threads 14-4
Locking 14-14
Concurrent Access 14-14
Locking 14-14
Types of Locks 14-15
Deadlock 14-19
xii SG 3605
Relational Concepts and SQL Programming
Materials 15-3
Schema 15-6
Storage-Areas 15-7
SG 3605 xiii
Relational Concepts and SQL Programming
xiv SG 3605
Relational Concepts and SQL Programming
Course Description
Objectives:
To prepare the student to write, test, and debug SQL programs.
Audience:
Application programmers, systems analysts, systems designers, and support personnel
Prerequisites:
Extensive COBOL programming experience in the 1100/2200 environment
Key Topics:
• Introduction to RDMS
• SQL command set
• Embedding SQL in programs
• Error handling and debugging
• IPF SQL
• MAPPER Relational Interface (MRI)
EL3605 5 DAYS
SG 3605 XV
Relational Concepts and SQL Programming
There are written and/or laboratory practices at the end of most modules to help you
understand the material presented. Two modules, modules 3 and 9, contain laboratory
exercises in the middle of the module.
Learning Environment
This course is designed to provide you with the background and skills to manipulate an
RDMS 1100 database. The concepts of relational database are introduced from a
programming viewpoint. The SQL command set is used to enable you to access and
manipulate relational data through COBOL host language programs. The IPF SQL and
MAPPER Relational Interface are introduced as other ways to access RDMS 1100
databases. Lab exercises provide you with the opportunity to practice coding skills in
relational data creation, retrieval, and updating, and use of alternative interfaces to
RDMS 1100.
Trademark Information
xvi SG 3605
Relational Concepts and SQL Programming
OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide 7831 0778-000
OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual 7830 8160-002
OS 1100 UDS Relational Data Management System (UDS RDMS 1100) Administration
Guide 7831 0760-002
Note: Consult the Series 1100 and 2200 Systems Product Information Library
Directory, if needed, for specific levels of these documents and other Unisys
documentation. The proper level of a document is needed to match release of
installed software for your system.
SG 3605 xvii
Relational Concepts and SQL Programming
Agenda
Day 1
Introduction
Day 2
More WHERE Clause Operators
Day 3
Grouping Functions
Joining Tables
Day 4
Data Manipulation
Day 5
Views
xviii SG 3605
1
Introduction
Introduction
Module 1
Introduction
Materials 1-3
SG 3605 1-1
Introduction
Module Objectives
- Correctly use the terms to describe a table, primary keys, tuples, and the
operations that can be performed on tables.
Benefit Statement
It is important that you have a few, fundamental concepts and terms in order to visualize
and work with a relational database. You will realize the several options you have for
accessing the database and understand how these options are implemented.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
SG 3605 1-3
Introduction
Relational Database
A database in which all the data is represented by tables. Table 1-1 illustrates a table.
Schema
- Entity
- Table (relation)
Column (attribute)
- Row (tuple)
- Primary key
Data value
Secondary key
1-4 SG 3605
Introduction
Operations on Tables
There are three basic types of operations that can be performed on tables:
These are the formal names for operations in the database literature. They are not the
commands used in SQL to perform the operations, but are listed to illustrate the kind of
processing that is possible.
These three operations may be combined to produce the exact data desired.
SG 3605 1-5
Introduction
RESTRICT Operation
The RESTRICT operation extracts a subset of the rows of an existing table. The desired
rows are selected by stating the conditions that the data values in specified columns must
satisfy. RESTRICT creates a horizontal subset of the existing table. From Table 1-2 we
extract only the winners from 1880 to 1885 creating Table 1-3.
1-6 SG 3605
Introduction
PROJECT Operation
The PROJECT operation extracts a subset of the columns of an existing table. The
desired columns are selected by stating the column names.
PROJECT creates a vertical subset of the existing table. Table 1-5 is produced from Table
1-4 by projecting the YEAR, CLUB and MANAGER columns.
SG 3605 1-7
Introduction
JOIN Operation
=
The JOIN operation creates a virtual table by combining the columns from two or more
tables. The tables must each have a column the values of which come from the same
domain (have the same data type). The user specifies which columns to use for the JOIN
and how to compare them.
Tables 1-6 and 1-7 are joined to display for each year the year, club, and nickname in
Table 1-8.
1-8 SG 3605
Introduction
SG 3605 1-9
Introduction
User Intertecs
Universal
Data System
RDMS
The Unisys database management software that organizes data for user applications in
an integrated information management system and in individual files. UREP also
maintains the repository.
On-line data manager for all three UDS data models. Provides control for common
functions such as 1/0, locking, queuing, memory management, and recovery.
A suite of Unisys software products for database management, data processing, and
database application development. Together with other software components and database
software products, UDS provides an integrated environment for control, maintenance, and
recovery of user databases.
1-10 SG 3605
Introduction
Interfaces to RDMS I
RDMS
• Figure 1-2 describes the interfaces discussed and illustrated in this course.
- ALLY Software
- SQL*Plus 1100
- SQL*Report 1100
- SQL*OCI 1100
SG 3605 1-11
Introduction
Interfaces to RDMS II
Application programs
— SQL commands can be placed in ASCII COBOL and ASCII FORTRAN programs
• IPF SQL
— SQL files can be created and run in IPF to execute a series of SQL commands
1-12 SG 3605
Introduction
- Queries = SELECT...FROM...
SG 3605 1-13
Introduction
Exercise 1-1
a. relation
b. tuple
c. attribute
d. primary key
1-14 SG 3605
2
Retrieving Data with
the SELECT Statement
Retrieving Data with the SELECT Statement
Module 2
Retrieving Data with the SELECT Statement
Materials 2-3
SG 3605 2-1
Retrieving Data with the SELECT Statement
Module Objectives
Benefit Statement
The most basic SQL command is the SELECT query specification. You will need this
command to retrieve information from your database. It will be used alone or in
combination with other commands. A few simple SELECT command clauses will give you
a great deal of power for your database accesses.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 2-3
Retrieving Data with the SELECT Statement
SQL Queries
The SELECT statement is used to retrieve data from a table in the database.
• Variations of the SELECT statement are used to perform all table operations
- Restrict
- Project
Join
• Can SELECT
- Entire table
Some columns
Some rows
2-4 SG 3605
Retrieving Data with the SELECT Statement
SG 3605 2-5
Retrieving Data with the SELECT Statement
SELECT Command
Example:
SELECT *
FROM DEPT
2-6 SG 3605
Retrieving Data with the SELECT Statement
Selecting Columns
Example: Display the department name, location, and department number, in that order,
of all departments in the DEPT table.
Example: Display the department name and department number columns from the
DEPT table.
DNAME DNO
ACCOUNTING 100
SALES 200
MARKETING 300
DISTRIBUTION 400
SG 3605 2-7
Retrieving Data with the SELECT Statement
• The DISTINCT keyword is used to suppress duplicate values in a column selected for
output.
Example: Display the different types of jobs in all the departments without repetition.
JOB
ACCOUNTANT
CLERK
MANAGER
PRESIDENT
SALESREP
• If more than one column is selected, DISTINCT selects unique combinations of values
in those columns
DNO JOB
100 ACCOUNTANT
100 CLERK
100 MANAGER
200 CLERK
200 MANAGER
200 SALESREP
300 PRESIDENT
400 CLERK
400 MANAGER
2-8 SG 3605
Retrieving Data with the SELECT Statement
SELECT select-list
FROM tablename
WH ERE condition
Example: Display the employee information for all employees named Hyde.
SELECT *
FROM EMP
WHERE ENAME = 'HYDE'
ENAME JOB
FOSTER SALESREP
BROWN CLERK
WOODWORTH MANAGER
MARTIN SALESREP
ADAMS SALE SREP
FORD SALESREP
SG 3605 2-9
Retrieving Data with the SELECT Statement
• = Equal
2-10 SG 3605
Retrieving Data with the SELECT Statement
Columns named in the WHERE clause must belong to the table named in the FROM
clause.
— The values tested in the condition do not need to be displayed in the resulting
table
SG 3605 2-11
Retrieving Data with the SELECT Statement
SELECT select-list
FROM tablename
WHERE condition
ORDER BY sort-specification-list
Example: Display the employee information for department 400 in alphabetical order by
employee name
2-12 SG 3605
Retrieving Data with the SELECT Statement
Arithmetic Expressions
Arithmetic expressions can be used to display data that is derived from values in columns
of the select-list. An expression is composed of column names and constant numeric
values connected by an arithmetic operator.
+ ADD
- SUBTRACT
* MULTIPLY
/ DIVIDE
Example: Display the names and yearly salaries of all employees in Department 400.
ENAME
JOHNSON 11700
SMITH 12600
HYDE 34800
SG 3605 2-13
Retrieving Data with the SELECT Statement
Order of Precedence
In evaluating expressions, the order of precedence of operators is as follows:
Equal precedence operations, like addition and subtraction, are performed in order, left to
right as encountered.
Parentheses are used to clarify or change the order of operations. Nested parentheses are
evaluated beginning with the innermost and working outward.
Example:
SAL + COMM * 12 - 200 is evaluated as
2-14 SG 3605
Retrieving Data with the SELECT Statement
Exercise 2-1
Write the SQL statement necessary to access either the EMP or DEPT table to retrieve
the following information. Indicate which records will be displayed.
1. Display the department name and department number for the department in
Atlanta.
CAI)
2. Display the names of all employees whose total compensation is more than $2600
per month.
„„•
3. Display the name, monthly salary, daily salary, and hourly salary for all
employees. Assume the SAL column is monthly salary and that there are
22 working days in a month and 8 hours in a working day.
SG 3605 2-15
3
Using SQL in
Application Programs
Using SQL in Application Programs
Module 3
Using SQL in Application Programs
Materials 3-3
SG 3605 3-1
Using SQL in Application Programs
3-2 SG 3605
Using SQL in Application Programs
Module Objectives
Upon completion of this module, you should be able to
- Describe the flow of processing in a host program that accesses RDMS 1100.
- Use the BEGIN THREAD and END THREAD commands to establish and
terminate a thread with UDS Control.
- Use host or embedded (ESQL) variables to pass parameters between RDMS and
host programs.
Benefit Statement
The SELECT command you have already seen is standard SQL. If you use an interactive
interface like IPF SQL, you would enter the command just as you have seen it.
However, if you are not performing ad hoc queries and need to further manipulate the
.data after it has been retrieved, your SQL commands must be embedded in a third-
generation language (3GL) program. This module tells you how to incorporate SQL into
COBOL or FORTRAN programs in the 1100/2200 environment. As a programmer, you
need to be familiar with the interfaces between RDMS and UDS and your 3GL program.
Materials
• OS 1100 Universal Compiling System (UCS) COBOL Programming Reference Manual
Volume 2: Compiler and System Interface
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 3-3
Using SQL in Application Programs
You have seen that a SELECT command returns a whole table or a subset of a table. A
third-generation language (3GL) program typically deals with only one record or row of
the returned table at a time. How to transfer one record of the returned table to the
program, and how to code program variables that reference them are important issues.
3GL Interfaces
Unisys provides two interfaces for 3GL programming languages - an interpretive SQL and
embedded SQL or ESQL.
The Embedded SQL interface, which is only available from UCS COBOL, fully conforms
to the SQL standard and provides some extensions as well. Each SQL command begins
with an embedded SQL prefix, EXEC SQL, and ends with an embedded SQL terminator,
END-EXEC. The embedded SQL commands are fully compiled along with the host
program. Execution of ESQL commands will be significantly faster than their interpreted
counterparts. This is particularly noticeable in program loops. Interpretive and embedded
SQL commands can be used in the same program.
Several other differences exist between the two implementations. ASCII COBOL and UCS
COBOL will be used to illustrate these differences.
3-4 SG 3605
Using SQL in Application Programs
- Unisys extensions
• 3GL interfaces
- Interpretive SQL
Embedded SQL
- Provide recovery
SG 3605 3-5
Using SQL in Application Programs
Table 3-1 groups the standard SQL commands that will be covered in this course. Unless
noted by the asterisk in Table 3-1, RDMS supports the SQL 89 form of the command and,
in many cases, offers one or more extensions to it. Table 3-1 is derived from the SB4R4
system release, which is release 5R3 of RDMS.
Refer to the OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual for more the details on the supported extensions for the
above commands.
Function Command
Miscellaneous GRANT
WHENEVER (ESQL) *
* RDMS does not support the SQL 89 form of this command and offers
no extensions to it
3-6 SG 3605
Using SQL in Application Programs
Table 3-2 lists the SQL extensions available from Unisys that will be covered in this
course.
Function Command
SG 3605 3-7
Using SQL in Application Programs
ESQL ISQL
Source: UCS COBOL Source: ASCII COBOL
@UCOB @ACOB
Dynamic/
Static @MAP
Linking
@XQT @XQT
RDMS
3-8 SG 3605
Using SQL in Application Programs
Source
- Place embedded UCOB SQL (ESQL) commands in packets delimited by EXEC SQL
and END-EXEC.
- UCS C - include the system header file rsa.h, then call the rsa function
Compilation
- Interpreted commands are not compiled (no prior creation of tables needed)
Collection or Link
Execution
- Interpretive SQL commands are compiled, then executed if no syntax errors exist
SG 3605 3-9
Using SQL in Application Programs
Interpretive SQL
Format 1
Format 2
Syntax Notes
• The item err-stat defines an error status returned by RDMS for an SQL command;
normal completion is 0000.
• The item aux-info contains useful information about the error status.
3-10 SG 3605
Using SQL in Application Programs
ISM Example
DATA DIVISION.
WORKING-STORAGE SECTION:
01 rcom.
05 rlin PIC X(40) OCCURS 10 TIMES.
01 err-stat PIC 9(4).
01 aux-info PIC S1(36).
•
PROCEDURE DIVISION.
SG 3605 3-11
Using SQL in Application Programs
Embedded SQL
Format 1
EXEC SQL
SQL-COMMAND
END-EXEC.
Format 2
SQL-COMMAND.
Syntax Notes
• Format 1
- Delimit packets for the compiler with EXEC SQL and END-EXEC
• Format 2
- For five commands that do not require the packet delimiters (packet is optional)
- BEGIN THREAD
- END THREAD
- COMMIT
- ROLLBACK
- GETERROR
3-12 SG 3605
Using SQL in Application Programs
- DECLARE CURSOR
USE DEFAULT
- WHENEVER
- SET STATISTICS
• Must be placed physically ahead of other commands they affect in the source program
Note: This course will address static ESQL only. Dynamic ESQL provides a subset of
SQL commands, typically involving a database update, that are not processed
at compile time, but at execution time. Dynamic ESQL is the functional
equivalent of the ENTER MASM interpretive interface for COBOL. A program
can contain a mixture of both static ESQL and interpretive SQL; this fact may
be important in a migration to ESQL from ISQL.
SG 3605 3-13
Using SQL in Application Programs
ESQL Example
Notice the SQL commands in the Procedure Division illustrated below that do or do not
require ESQL packets. Part of required Data Division is illustrated; it will be more fully
discussed with error handling in this module.
DATA DIVISION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
EXEC SQL
WHENEVER SQLERROR GO TO :rdms-err-para
END-EXEC.
EXEC SQL
DECLARE employee CURSOR SELECT * FROM emp
END-EXEC.
rdms-err-para.
3-14 SG 3605
Using SQL in Application Programs
Constants
String Literals
ENTER MASM 'ACOB$RDMR' USING 'SQL Command' ... uses a string literal to pass the
command to RDMS.
Numeric Literals
Numeric literals are either integers or real numbers. Real numbers contain a decimal
point, but integers do not. You can write them with or without the leading sign. Numeric
literals cannot be longer than 21 digits. This limit does not include either the sign or the
decimal point.
NULL Values
A data item that has no assigned value has a NULL value. NULL values are distinct
from zero values or spaces. NULLs are used instead of inserting dummy values that may
be mistaken for assigned values. NULL values may not be used in arithmetic or Boolean
expressions. There are special functions that test for NULL values.
3. Names must begin with a letter, and cannot end with an underscore.
6. The above rules can be ignored if the name is enclosed in double quotes.
Table 3-3 illustrates several valid and invalid names for database objects.
SG 3605 3-15
Using SQL in Application Programs
Valid Invalid
sales_reps sales-reps
PURCHASE_PRICE PURCHASE*PRICE
"TABLE" TABLE
Every literal or variable has a data type. A data type associates a fixed set of properties
with a value. It also restricts the values the data item may contain
A value in the database takes on the data type of the column to which it belongs. When a
table is created or modified, the column is assigned a data type. When a query is
executed and values are returned to your program, the program variables into which the
values will go must correspond to the data type of the database item.
CHARACTER (size)
NCHARACTER (size)
CHARACTER data types contain ASCII or any other 9-bit data. Character(5) specifies a
five-position character string. A corresponding COBOL program variable should be
defined as PIC X(5).
3-16 SG 3605
Using SQL in Application Programs
The exact numerical data types and their definition formats are:
DECIMAL [(sign-and-digits-precisionLscaleN
NUMERIC [digits-precisionkscale)]
INTEGER
SMALLINT
The value of sign-and-digits-precision specifies the total number of digits in the column
including the leading sign. This must be an integer between 2 and 22, inclusive. For
example, DECIMAL(6.2) allows 6 positions, 2 of which are decimal. A corresponding
COBOL program variable would be defined as PIC S999V99. Decimal(4) should be
declared as PIC S9(3).
The digits-precision is the number of digits in the column and must be no larger than 11.
The scale is a non-negative integer that indicates the number of digits for the fractional
portion of the column This can be no larger than digits precision - 1 for NUMERIC and
no larger than sign-and-digits-precision -1 for DECIMAL.
A period can be used instead of a comma between digits precision and scale.
REAL
FLOAT [(binary-precision)]
DOUBLE PRECISION
The value of binary precision must be a positive integer less than 60. It defines the
minimum binary precision in bits.
Consult the OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual for more information on the numerical data types.
SG 3605 3-17
Using SQL in Application Programs
3-18 SG 3605
Using SQL in Application Programs
Before you can write a program that incorporates SQL commands in the 1100/2200
environment, there are a few more terms that must be defined. They relate your program
to its environment.
Cursor
The definition of the data to retrieve. Used only to return results to a program.
Currency
Currency indicates which row of the cursor is being accessed. This pointer is needed as a
way to keep track of the rows being returned to a COBOL or FORTRAN program, one at
a time.
Application Group
A software partition that acts as an independent database system and has its own system
and data files and its own recovery environment. Applications groups do not share data
with other application groups and there is no coordinated locking or recovery between
application groups. As a programmer, you need to know the name or alias of the
application group to which your program belongs.
Thread
A thread is a work session established with UDS. It defines your UDS environment.
Each thread exists in one and only one application group.
Program Flow
Figure 3-2 illustrates the typical program flow of control and some of the SQL commands
that are involved.
SG 3605 3-19
Using SQL in Application Programs
Start
BEGIN THREAD
DECLARE CURSOR
SELECT
Loop
Process Record
END THREAD
End Program
3-20 SG 3605
Using SQL in Application Programs
- Specifies application group or alias name for application group to which thread
belongs
• Retrieval options
— READ/RETRIEVE
• Message options
Examples:
SG 3605 3-21
Using SQL in Application Programs
- Defines a conceptual table containing the rows returned before they are passed to
the program, one row at a time
Examples
DECLARE ci CURSOR
SELECT dname, dno FROM dept
ORDER BY dno
3-22 SG 3605
Using SQL in Application Programs
OPEN cursor-name
• Opening the cursor evaluates the query in the DECLARE CURSOR command
- Sets the cursor's currency pointer to just prior to the first row
Example:
SG 3605 3-23
Using SQL in Application Programs
FETCH Command
FETCH retrieves the next row of the cursor relative to the currency pointer
— First FETCH retrieves the first row returned based on the cursor definition
• Where variable-specification-list is
A list of placeholder variables, one for each column in the cursor (ISQL)
— A set of embedded variables, one for each column in the cursor (ESQL)
ISQL Example:
ESQL Example:
EXEC SQL
FETCH dept_cursor INTO :rr-dno, :rr-dname, :rr-loc
END-EXEC.
3-24 SG 3605
Using SQL in Application Programs
Figure 3-3 illustrates how data is passed between a program and RDMS.
Note: Only interpretive SQL requires the placeholder variables, $P1, $P2,and $P3.
The host program variable's size and data type is determined from the RDMS column it
corresponds to. You can find this information in the table definition. The size and data
type of your host program variables must match the size and data type of the table
declarations .
You need a host program variable for each column you are inserting values into,
retrieving values from, or comparing values against.
The ASCII COBOL (ACOB) compiler limits the number of parameters passed on an
ENTER MASM call to 31.
The UCS COBOL (UCOB) compiler limits the number of parameters passed to 250.
SG 3605 3-25
Using SQL in Application Programs
ISQL
Requires
Placeholder $P1 $P2 $P3
Variables
WORKING STORAGE.
01 record-retrieved.
05 rr-dno PIC 9(4).
05 rr-dname PIC X(12).
05 rr-loc PIC X(10).
3-26 SG 3605
Using SQL in Application Programs
END THREAD
SG 3605 3-27
Using SQL in Application Programs
• Table specification
Examples:
3-28 SG 3605
Using SQL in Application Programs
Exercise 3-1
1. Identify which variable names are valid and which are invalid in RDMS.
MY-TABLE
EMPCURSOR
Table*2
Another Cursor
2. If the following columns were defined in a table called CAR SALES, how would the
program variables into which they were to be retrieved be declared?
REG_NUMBER DECIMAL(8)
MAKE_AND_MODEL CHARACTER(10)
DATE_PURCH DECIMAL(7)
SALE_PRICE DECIMAL(6)
REP_CODE SMALLINT
COMMISSION NUMERIC(7,2)
3. Put a number before each command below to indicate the order in which they would
appear in an application program.
OPEN CURSOR
END THREAD
DECLARE CURSOR
BEGIN THREAD
FETCH
SG 3605 3-29
Using SQL in Application Programs
4. Code the complete ESQL or ISQL command to define a cursor that selects the
employee number, employee name, and total compensation from the EMP table.
5. Code the ESQL or ISQL call to fetch the rows retrieved in item 4 above. What
assumptions must you make about the WORKING-STORAGE section of your program?
3-30 SG 3605
Using SQL in Application Programs
Lab 3-1
Your instructor will discuss your particular lab set-up and requirements with you.
Appendix A is provided to keep track of necessary system usage information.
Using the ACOB or UCOB skeleton provided, write a program to display the employee
number, name, position, and hire date of all employees in Department 200. You must
complete the working storage section and the appropriate ISQL or ESQL commands.
A hard copy of the ACOB skeleton for the interpretive SQL interface (ISQL) is in
Appendix C of this student guide. The ACOB program example in Appendix D should help
you decide what work needs to be done.
Similarly, a hard copy of the UCOB skeleton for working with embedded SQL (ESQL) is
in Appendix E, and is followed by a complete program in appendix F.
In lab you will edit the on-line skeleton program, compile, and execute the program. The
on-line program skeleton names are ACOBSKEL and UCOBSKEL. Information on
compiling, collecting or linking, and executing your program can be found in Appendix A.
The flow chart in Figure 3-4 illustrates the basic steps for the examples in the appendixes
which read and display all of the records in the employee table EMP. Your program logic
will be very similar.
SG 3605 3-31
Using SQL in Application Programs
Start
BEGIN
THREAD
DECLARE
CURSOR
OPEN
CURSOR
END
THREAD
3-32 SG 3605
Using SQL in Application Programs
— End-of-cursor = 6001
SG 3605 3-33
Using SQL in Application Programs
You can test the value of error-status variable then decide how to handle the error. A
status equal to 6001 indicates a no-find or an end-of-data condition. After fetching from a
cursor, this is not an error. Continue to the next processing step.
The auxiliary information variable returns additional information about the error status.
When the error status is not between 6000 and 6999, it returns the column number in the
command where the syntax error was located. When the error status is between 6000
and 6999, auxiliary information contains other command status and rollback information.
ERROR-PRINT
DISPLAY 'RDML COMMAND = 'ROOM
DISPLAY 'ERROR STATUS = ERR-STAT
DISPLAY 'ERROR COLUMN = 'AUX-INFO
FORTRAN Example:
rcom = command-string
CALL F$RDMR (rcom, erstat, auxinf)
IF (erstat .NE. '0000') THEN
PRINT '(1X,A)', 'RDMS command: ',rcom
PRINT '(A26,I3)', 'Error Found at Column ', auxinf
PRINT '(A16,A4)', 'Error Status: ', erstat
ENDIF
3-34 SG 3605
Using SQL in Application Programs
GETERROR Command
• Unisys extension
• Details on variable-list
- Each host program variable must hold at least 132 ASCII characters
• Retrieved message must be printed or moved before executing the next SQL command
ESQL Example:
SG 3605 3-35
Using SQL in Application Programs
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
error-para.
MOVE 0 TO err-end-flag.
PERFORM do-geterror UNTIL err-end-flag NOT = 0.
ENTER MASM 'acob$rdmr' USING 'END THREAD ;',
error-stat, aux-info.
DISPLAY "Abnormal end of program." UPON PRINTER.
STOP RUN.
do-geterror.
MOVE 'GETERROR INTO $P1, $P2, $P3, $P4, $P5;' TO rcom.
ENTER MASM 'ACOB$RDMR' USING rcom, err-stat,
aux-info, err-s(1), err-s(2), err-s(3), err-s(4), err-s(5).
PERFORM print-error VARYING i FROM 1 BY 1 UNTIL i > 5 .
print-error.
If err-s(i) NOT = SPACE
DISPLAY err-s(i) UPON PRINTER
ELSE
MOVE i
MOVE 1 TO err-end-flag.
3-36 SG 3605
Using SQL in Application Programs
The variable SQLCODE must be declared by that name in the Data Division of your
program. It holds the completion status of each executed SQL command. A value of 0
indicates normal completion, +100 means no data or no more data, and a negative value
indicates an error has occurred.
You will use the WHENEVER command to transfer control to a particular paragraph
should a no-find or error condition occur when executing an SQL command.
A new section must define variables you use in ESQL statements. This section, also a part
of the SQL standard, is written in the Working-Storage Section as an embedded
command. It is defined with the Begin Declare command.
A UCOB compiler option, LEVEL/SQL, can be used on the compiler call to flag
nonstandard commands. You can enter this keyword option as you would any other, for
example:
@UCOB,ES SQL.LAB3-1/UCOBSKEL,TPKUCOB„,NARROW,LEVEUSQL
SG 3605 3-37
Using SQL in Application Programs
• WHENEVER command
3-38 SG 3605
Using SQL in Application Programs
WHENEVER Command
• Detected conditions
— NOT FOUND means SQLCODE = +100, a "not found" status returned by RDMS
• Action to take
• A WHENEVER command affects all ESQL commands that follow it, until another
WHENEVER command for the detected condition
Examples:
EXEC SQL
WHENEVER SQLERROR GO TO rdms-error-para
END-EXEC.
SG 3605 3-39
Using SQL in Application Programs
• Marks a data area for variables used in SQL commands in the COBOL Working-
Storage Section
• RDMS uses variables automatically to return status information for each SQL
command, except GETERROR
3-40 SG 3605
Using SQL in Application Programs
WORKING-STORAGE SECTION.
•
EXEC SQL BEGIN DECLARE SECTION END-EXEC.
01 SQLCODE PIC S9(9) USAGE BINARY.,
01 RDMCA.
05 ERROR-STATUS PIC 9(4). CO,
05 AUX-INFO PIC S9(9) USAGE BINARY.,
EXEC-SQL END DECLARE SECTION END-EXEC.
01 error-text.
05 err-s PIC X(132) OCCURS 5 TIMES.
01 err-index PIC 9.
PROCEDURE DIVISION.
EXEC SQL
WHENEVER SQLERROR GO TO rdms-error-para
END-EXEC.
rdms-error-para.
EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
DISPLAY '***SQLCODE = ' SQLCODE UPON PRINTER.
DISPLAY '***AUXILIARY INFO = ' AUX-INFO UPON PRINTER.
DISPLAY '***ERROR STATUS = ' ERROR-STATUS UPON PRINTER.
GETERROR INTO :err-s(1), :err-s(2), :err-s(3),
:err-s(4), :err-s(5).
PERFORM VARYING err-index FROM 1 BY 1 UNTIL err-index = 6
DISPLAY err-s(err-index) UPON PRINTER
END-PERFORM.
PERFORM end-thread-to-uds.
PERFORM termination-para.
SG 3605 3-41
Using SQL in Application Programs
Lab 3-2
Insert errors, such as too few program variables or inconsistent data types for program
variables to receive query results. Then execute the program to see the types of error
messages returned.
Of course, if you are working in ESQL most of your errors are detected and flagged at
compilation time. You should force several compile time errors in order to become familiar
with the error messages.
3-42 SG 3605
4
More WHERE Clause
Operators
More WHERE Clause Operators
Module 4
More WHERE Clause Operators
Materials 4-3
SG 3605 4-1
More WHERE Clause Operators
Module Objectives
Benefit Statement
In Module 2 you learned the elements of simple queries. This module builds upon that
module with extensions to the WHERE clause. You will now be able to construct WHERE
clauses that contain multiple conditions, test for NULL values, and use pattern matching.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 4-3
More WHERE Clause Operators
Testing Conditions
Multiple Conditions
You have already written simple queries using a single condition in Module 2. But in real
life, you must be able to express more complex sets of conditions. In SQL you can
construct a query that is as complex as you require by combining conditions with logical
operators.
— AND
- OR
NOT
- IS [NOT] NULL
- [NOT] LIKE
• You can control the appearance of the output using ORDER BY.
4-4 SG 3605
More WHERE Clause Operators
NULL Values
10 * NULL is NULL
• There are special ways to handle displaying NULL values using indicator variables
SG 3605 4-5
More WHERE Clause Operators
IS [ NOT ] NULL
4-6 SG 3605
More WHERE Clause Operators
Logical Operators
- Valid
- Invalid
• The word WHERE appears only once, no matter how many conditions are used
SG 3605 4-7
More WHERE Clause Operators
AND
Table 4-1. AND Truth Table
OR
Table 4-2. OR Truth Table
NOT
Table 4-3. NOT Effect
NOT
TRUE FALSE
FALSE TRUE
NULL NULL
4-8 SG 3605
More WHERE Clause Operators
BETWEEN Operator
• Logical equivalent of
Example:
SG 3605 4-9
More WHERE Clause Operators
Boolean NOT 5
AND 6
OR 7
Table 4-4 shows the operators RDMS accepts and the order of precedence each has been
assigned. You can use parentheses to change the order of precedence as needed.
4-10 SG 3605
More WHERE Clause Operators
LIKE Operator
— Case-sensitivity is significant
• Two characters, "%" and "_ , behave as "wild cards" for the LIKE operator
• Search for strings that include "%" or "_" by preceding them with escape character
specified
SG 3605 4-11
More WHERE Clause Operators
LIKE Examples
Example 1. List the employee information from the EMP table for employees whose
names have five characters..
Example 2. List the employee information from the EMP table for all employees whose
last names begin with "W'.
Example 3. List employees from EMP whose names have an "A" as the third from last
character.
Example 4. List employee information from the EMP table for employees whose names
have an "A" followed by an "S".
Example 5. From a real estate system, search for a house with a "9% assumable" in
the description column.
4-12 SG 3605
More WHERE Clause Operators
Exercise 4-1
Indicate whether the following queries have valid or invalid syntax. If invalid, correct the
query so that it has valid syntax.
SG 3605 4-13
More WHERE Clause Operators
Write queries to display all the employee information for employees who satisfy the
following conditions:
373
7. All employees hired after Jan 1, 1986 whose total compensation per month is more
than $2500.
8. All sales reps and accountants who earn less than $2300 and more than $2000.
( (- 1'/-••
f;- .3,- • •
ttk
10. All employees not in department 100 holding any job but president or clerk.
/„..c
4-14 SG 3605
More WHERE Clause Operators
Lab 4-1
For this lab you will begin with the program as you completed it from Lab 3-1.
You will modify it so that it now uses a more complex query. The two queries that you
wrote as answers to questions 7 or 8 in Exercise 4-1 are good candidates for the requested
"complex query" for this exercise.
SG 3605 4-15
5
The IPF SQL Interface
The IPF SQL Interface
Module 5
The IPF SQL Interface
Materials 5-3
SG 3605 5-1
The IPF SQL Interface
Module Objectives
• Invoke and use the IPF SQL interface to RDMS for simple queries.
Benefit Statement
The IPF SQL interface is easy to learn and use; it will greatly enhance your mastery of
the SQL syntax. You will use this interface for ad hoc queries as well as for prototyping
applications being developed.
Materials
OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
SG 3605 5-3
The IPF SQL Interface
Type "SQL command ?" to obtain command information, where the command is
represented by the first one or two keywords
— Type "?" to obtain more information until you receive the end of message notice
5-4 SG 3605
The IPF SQL Interface
SQL commands can be used with IPF commands in an .IPF session. You can:
- All commands must be preceded by SQL to tell IPF they are SQL commands
• Continue a command to the next line by using the IPF continuation character.
Invoking IPF
- Line mode
- Full-screen mode
• Any IPF or SQL command may be entered following the >-C-> prompt.
• Figure 5-1 illustrates the sequence of commands executed in a typical IPF SQL
session.
SG 3605 5-5
The IPF SQL Interface
Invoke IPF
Set Datamanager
BEGIN THREAD
Other SQL
Commands
END THREAD
Exit IPF
5-6 SG 3605
The IPF SQL Interface
>@IPF TPF$.
>IPF 1100 5R1 02/19/93 09:26:11
>-C->$DATAMANAGER := RDMS
>-C->SQL BEGIN THREAD Ti FOR UDSSRC READ
>-C-->SQL USE DEFAULT QUALIFIER ROSVSCHEMA1
>The USE command completed successfully.
>-C->SQL SELECT * FROM DEPT WHERE DNO >= 200
>-DNO- ---dname---- ---loc----
> 200 SALES NEW YORK
> 300 MARKETING DETROIT
> 400 DISTRIBUTION PRINCETON
>3 records were selected.
>-C->SQL SELECT ENAME, HIREDATE, SAL * 12 FROM EMP &
>-C--&>WHERE HIREDATE BETWEEN 890101 AND 900101
>--ename--- hiredate
>TURNER 890515 10800
>JOHNSON 891221 11700
>FORD 890316 28500
>3 records were selected.
>-C->SQL END THREAD
>-C->logoff
>END IPF
• Start of entry (SOE) character is represented by the greater than symbol (>) above
SG 3605 5-7
The IPF SQL Interface
• How is it created?
Screen mode is recommended for entering and editing text, and line mode for
executing the file or interactive SQL commands
- Begin a new file by typing "NEW filename" on the command line at the top
- If the file has already been saved, then you must use the "REPLACE" (REPL)
command to save it
5-8 SG 3605
The IPF SQL Interface
To execute a file, type "SQL FTLE filename" on the command line. When you submit a
FILE command, IPF SQL accesses the file or element specified, then reads and executes
the SQL commands. It executes all the commands even if it detects errors as it proceeds.
IPF returns error messages interspersed with the results of the commands it executes.
Files are better executed in line mode since the screen otherwise returns to the previous
edit screen, overlaying the query results. To enter line mode, type "MODE LINE" on the
command line.
SG 3605 5-9
The IPF SQL Interface
Fifth command copies the contents of the output file to the IPF workspace for
viewing
5-10 SG 3605
The IPF SQL Interface
Lab 5-1
1. Invoke the on-line help facility to obtain information on SQL in general (SQL ?) and
SQL commands (SQL command-name ?) in particular.
2. Enter a SELECT command before a BEGIN THREAD to see the kind of error
messages that are returned.
c. Display the employee number, name, position, and hire date of all employees in
department 200.
4. Create, save, and execute an IPF SQL file that includes any two queries from
Exercise 4-1.
SG 3605 5-11
6
The MAPPER
Relational Interface
The MAPPER Relational Interface
Module 6
The MAPPER Relational Interface
Materials 6-3
SG 3605 6-1
The MAPPER Relational Interface
Module Objectives
• Invoke the MRI run to retrieve and manipulate data from an RDMS database.
Benefit Statement
An alternative to the IPF interface is the MAPPER Relational Interface (MRI). You will
find this interface even easier to use to obtain ad hoc reports, with its menu-driven
approach. Of course, MRI supports more sophisticated database accesses as well, and you
will be introduced to SQL in MAPPER runs as well.
Materials
SG 3605 6-3
The MAPPER Relational Interface
- Data reduction
- Graphics
- Display/store of data
6-4 SG 3605
The MAPPER Relational Interface
MAPPER
Request
Manager
MRI
Remote
Execution
SYBASE
DMSII
DB2 INFORMIX
Oracle
SG 3605 6-5
The MAPPER Relational Interface
Invoking RDI
Sign-on to MAPPER
Figure 6-2 shows RDI entered on the top line, the control line.
rdi
Unisys MAPPER System
• EDMAP 35R1 EA18 *
Station: 8417 System: 1
User-id: DAA
Cabinet: 8
6-6 SG 3605
The MAPPER Relational Interface
• Selection choices
Utilities menu allows you to change the design, restrict table access, delete tables,
or work with views
RDI
SG 3605 6-7
The MAPPER Relational Interface
• Selection choices
RDI
Environment menu_
Enter cabinet number for results -> 8 Drawer -> a
Generate SQL syntax on updates? -> y
New default qualifier -> ROSUSCHENA1
New default version -)
Place cursor here and press XNIT ->
6-8 SG 3605
The MAPPER Relational Interface
RDI
RDI menu
Tab to your choice and press XMIT
Select ►
Join ►
Insert 00
Delete 00
Update P
Create ►
SQL ►
Utilities menu P
Detailed help ►
Rev. Level 3R3
On the Select Menu pictured in Figure 6-6, the table name EMP was entered and all
columns selected with the *. The cursor was transmitted from the XMIT position.
RDI
Select menu
Retrieve rows from a single relational table.
Table name ->
Column names -> *
Where clause -)
Retrieve all rows? -> y If no, how many? -> 1
Number of rows to skip -> 8 Null Symbol -) *
Extra options? -> n
Place cursor here and press XMIT ->
Press XMIT from here for detailed help ->
SG 3605 6-9
The MAPPER Relational Interface
The results are shown in Figure 6-7 from the generated SQL command.
Pressing F3 (ShwSQL) on the previous menu produces the SQL shown in Figure 6-8.
Pressing F3 (SveSQL) from this menu allows you to save the generated SQL in a report
for future use. This was not done in this example.
4 - RESULT
.DATE 19 FEB 93 18:26:12 REPORT GENERATION DAA
- RDI Select, SQL request
SELECT
FROM EMP
END REPORT
6-10 SG 3605
The MAPPER Relational Interface
In order to enter your SQL syntax directly, first position to SQL on the RDI menu as
pictured in Figure 6-9 and transmit.
RDI
RDI menu
Tab to your choice and press XMIT
Select ►
Join
Insert ►
Delete
Update ►
Create ►
SQL ►
Utilities menu
Detailed help
Rev. Level 3R3
On the SQL Menu, merely position the cursor to the XMIT and transmit.
RDI
- SQL menu .
You nag enter a report number, an SQL request name, or leave
both fields blank for freeform entry.
Report ->
SQL request -)
Process request immediately? -> n
Place cursor here and press XMIT ->
Press XMIT from here for detailed help ->
SG 3605 6-11
The MAPPER Relational Interface
On the free form report, enter your SQL statement as you would normally, ending it with —
a semicolon. Press XMIT to have the command processed and then Fl to continue. Figure
6-11 illustrates a SELECT command to be submitted.
END REPORT
5 - RESULT
.DATE 18:41:04 RID 19 FEB 93 DAA
* • . .HIREDAT.
*EMPNO. ENAME :JOB .MGR .DNO .E .SAL .COMM
*- .
5010 FOSTER SALESREP 5234 200 860714' 2700. 400
5146 BROWN CLERK 5234 208 871011 1000
5234 WOODWORTH MANAGER 5784 200 790219 3258
5437 MARTIN SALESREP 5234 200 870326 2500 300
5469 ADAMS SALESREP 5234 200 811102 2258 500
5942 FORD SALESREP 5234 280 890316 2375
END REPORT
6-12 SG 3605
The MAPPER Relational Interface
Exiting MAPPER
Place an X on the control line to exit MAPPER. Figure 6-13 shows the exit from the RDI
menu screen.
X RDI
RDI menu
Tab to your choice and press XMIT
Select 0-
Join 0-
Insert ►
Delete 0-
Update 0.
Create 0-
SQL ►
Utilities menu
Detailed help
help 0-
Rev. Level 3R3
SG 3605 6-13
The MAPPER Relational Interface
For more information, refer to the MAPPER System Run Design Operation Reference
Manual.
6-14 SG 3605
The MAPPER Relational Interlace
Figure 6-14 shows an example of a MAPPER run that will perform a simple fetch.
LINE! 1 FMTOP RD- - SHFTO, HLD CHRSR HLD LNO- UNDO- 11124 11.
.DATE 23 FEB 93 88:54:44 RID II 19 AUG 92 DAA
.RBN81 1881760
@LGN,I,Y,8,AJDMS READ
@FCH,8,A,ILREADyI RDMS \
'C * RoSyScHEMAl.DEPT'
8DSP,-0
@LGF,1RDMS
@GTO END .
@I:DSP,-0
END REPORT
Entering the name of the run on the command line produces the result shown in Figure
6-15.
SG 3605 6-15
The MAPPER Relational Interface
Lab 6-1
5. Select columns to display by typing a number in the first field, indicating the order in
which they should be displayed.
7. Delete the ? from the column name field. Tab to the WHERE field. Enter a ? and
transmit.
Press F3 to re-display the list of column names in a corner of the screen for reference.
Press Fl to continue.
9. Type the following in the WHERE field at the bottom of the screen.
10. Press XMIT then Fl to execute the query. The result is displayed.
6-16 SG 3605
The MAPPER Relational Interface
Lab 6-2.
Use the MRI interface to display the EMPNO, ENAME, and MGR columns of EMP where
the manager's empno is greater than the employee's empno.
SG 3605 6-17
7
Grouping Functions
Grouping Functions
Module 7
Grouping Functions
Materials 7-3
AS Clause 7-5
SG 3605 7-1
Grouping Functions
Module Objectives
Benefit Statement
The grouping functions in this module help answer the question, "What is the bottom line
here?" The GROUP BY clause lets you obtain summarized information, and its
companion HAVING clause refines the information you obtain. The AS clause provides
your report with a meaningful name. So what is the bottom line here? More power for
querying your database!
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
SG 3605 7-3
Grouping Functions
Grouping Functions
Grouping functions operate on a group or set of rows and return one row of summary
information for each group. The default group is the entire table, but other groups may
be specified.
- COUNT(*) Counts the number of rows in the table, including duplicates and
NULLS
7-4 SG 3605
Grouping Functions
AS Clause
select-expression AS title-name
A result of a function
Example: Display the names and yearly salaries of all employees in Department 400.
ENAME NEWSAL
JOHNSON 11700
SMITH 12600
HYDE 34800
SG 3605 7-5
Grouping Functions
Function Examples
Example 2. Compute the average salary for all employees in the company.
sal_avg
323.21428571428571429
Example 3. Compute the minimum and maximum salaries paid in the company.
minsal maxsal
900 5200
Example 4. What is the earliest date on which someone was hired and what was the
most recent date on which someone was hired?
781015 891221
7-6 SG 3605
Grouping Functions
Summarizing functions
Aggregate functions
- Set functions
• Functions which apply to a group of records return a single value for the group.
• The DISTINCT keyword used within a grouping function makes the function consider
only distinct values of the expression.
Do not specify DISTINCT or ALL for AVG and SUM functions, since ALL is
assumed.
Only MIN, MAX, and COUNT may be used with character columns
• MIN/MAX functions use the ASCII collating sequence to order and select
• NULL values are ignored in the evaluation of SUM, AVG, MAX and MIN.
SG 3605 7-7
Grouping Functions
COUNT Function
• Asterisk may be used to refer to all columns since all have the same number of rows
SELECT COUNT(*)
FROM EMP
14
Example 2. Count the number of different job titles in the EMP table.
SELECT COUNT(*)
FROM EMP
WHERE DNO = 400
7-8 SG 3605
Grouping Functions
A group is a set of records that have the same value in one or more columns.
The column(s) used to define the group must be identified in the GROUP BY
clause
- The grouping function in the SELECT clause then returns one row for each group
instead of one row for the whole table.
- Grouping functions.
— Literals
Valid Example
Invalid Example
Note: ENAME is not a grouping function and is not a column-name in the GROUP
BY clause and therefore returns an error.
SG 3605 7-9
Grouping Functions
• One group is formed for each unique combination of values in the GROUP BY
columns.
Example Count the number of people who hold each type of job in each department.
--dno-- ---job
300 PRESIDENT 1
200 SALESREP 4
100 CLERK 1
200 CLERK 1
400 CLERK 2
100 MANAGER 1
200 MANAGER 1
400 MANAGER 1
100 ACCOUNTANT 2
7-10 SG 3605
Grouping Functions
The WHERE clause can be used in conjunction with GROUP BY, to select rows from a
table upon which the grouping functions will be performed.
Example: Determine the average salary for accountants and sales representatives and
the number of each in each department.
--dno
100 2012.50 2
200 2456.25 4
Note: The average salary in this example was truncated to two decimal places.
SG 3605 7-11
Grouping Functions
HAVING Clause
• Specifies which groups should be returned, after the grouping functions have been
computed
Example:
1
(
:• •
7-12 SG 3605
Grouping Functions
• SELECT statements may contain WHERE, GROUP BY, and HAVING clauses
— FROM, WHERE, GROUP BY, and HAVING clauses identify the tables and rows
• Order of execution
1. The FROM clause creates a conceptual table from the tables to be used.
2. If there is a WHERE clause, rows not meeting its requirements are discarded
from the conceptual table made in step 1.
4. If there is a HAVING clause, rows not meeting its requirements are discarded
from the grouped table made in step 3.
Example:
SELECT DNO
FROM EMP
WHERE JOB = 'CLERK'
GROUP BY DNO
HAVING COUNT(*) >= 2
-dno-
400
SG 3605 7-13
Grouping Functions
Exercise 7-1
Using the functionality you have learned so far, write and execute the SQL statements
that will retrieve the requested data from the EMP and DEPT tables.
1. What is the average annual salary per job in each department? Recall the SAL
column is monthly salary.
fl-c)
V C- -„3-0
0
2. Display the department number and number of employees, who receive salaries only
in the entire company.
L'”
-t' '
0 tj
7-14 SG 3605
Grouping Functions
3. Compute the average, minimum, and maximum salaries of those employees who are
clerks or managers. Display the job titles and results.
4. Display the department number of departments that have more than one clerk.
JV i
L
SG 3605 7-15
8
Joining Tables
Joining Tables
Module 8
Joining Tables
Materials 8-3
Equijoins 8-4
Self-Joins 8-10
SG 3605 8-1
Joining Tables
Module Objectives
Benefit Statement
Thus far the queries you have created have dealt with one table. This module expands
your capabilities to involve more than one table. The data that you will need to access will
frequently be in more than one table.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 8-3
Joining Tables
Joins
• A join combines some or all columns and rows from two or more tables
• Types of joins
- Equijoins
- Cartesian joins
Self joins
— Non-equijoins
Equijoins
SELECT columns-from-tables-in-from-clause
FROM table 1, table2,
WHERE table l.column = table2.column
AND other-conditions
• An equijoin links rows from tables based on the equality of a common attribute
(column)
- The columns on which the tables are joined must have the same data type
• Figure 8-1 illustrates joining two tables based on the department number field (DNO)
EMP
DEPT
8-4 SG 3605
Joining Tables
Equijoin Examples
Example 1. For each employee display the employee name, department number,
name, and location.
Example 2.
Note: The second example will produce the same results as the first.
SG 3605 8-5
Joining Tables
Join Guidelines
• The WHERE clause specifies how to join the tables as well as any other selection
conditions as in single table queries.
• The "matching criteria" for the tables are called the join criteria
• More than one pair of columns may be used to specify the join condition between any
two tables
• If a column has the same name in both tables, it must be unambiguously referenced
as table-name.column-name in the SELECT or WHERE clause.
• The join is more efficient if the columns used to perform the join are indexed
• As many tables as desired may be joined together, although more than three may
cause performance slowdown
• If the WHERE clause does not use primary or secondary keys, the order of the tables
in the FROM list is 4xot important.
- Think of RDMS 1100/2200 as scanning the list from left to right
- Only the records "found" in the first table need to be joined with records in the
second and subsequent tables
Specify as the first table the one with the fewest expected "hits"
8-6 SG 3605
Joining Tables
Multi-Table Joins 1 11
• If joining more than two tables, specify a join criterion for each pair of tables being
joined.
• For joining n tables, there should be n-1 join criteria in the WHERE clause.
Example Assume there is a third table named AUX_EMP that contains personnel
information on each employee, such as GRADE, and having a primary
key of EMPNO. If you want to display the employee's name, grade,
position, department number, and location, the information can be
collected from EMP, DEPT, and AUX EMP. The three tables could be
joined with a query such as the following:
Note: The WHERE clause specifies the join conditions. Only a part of the resulting
table is shown for the above SELECT.
SG 3605 8-7
Joining Tables
Cartesian Joins
If you do not specify a join criterion, then RDMS does not know how you want the rows of
one table matched with the rows of the second table. The table that results is a Cartesian
product; every row of the first table is matched with every row of the second. This
produces a large table and is usually not what was wanted.
Cartesian joins exist for operational completeness, but when you get one, it is usually an
accident. You probably forgot to specify the desired join condition. The resulting table
has as many rows as the product of the number of rows in each table.
Example: Modify the previous equijoin example, omitting the WHERE clause and
displaying both department number fields. The query would be:
The table answering that query would have 14 x 4 or 56 rows. The top of the table would
appear as follows:
Note: There are four entries for each employee; one for each row of possible values
from the DEPT table.
8-8 SG 3605
Joining Tables
Table Aliases
• When joining a table with itself, it must be given aliases to clarify the column
references
Example 1
Example 2
SG 3605 8-9
Joining Tables
Self-Joins
Joins a table to itself, comparing values in two different columns with the same data
type.
• The column names in the select-list must be qualified with the alias to distinguish
which to display since the column names in the tables are identical.
Example: Display the number, name, and job of each employee, and the number, name,
and job of the employee's manager.
8-10 SG 3605
Joining Tables
Self-Join Guidelines
• Self-join is used to join one row in a table with another in the same table
• The columns specified in the join criterion must have the same data type
• The table must be given two aliases to identify which columns are coming from which
copy of the table
- Both the EMPNO and MGR columns contain the same type of information,
employee numbers as four numeric digits
- For each row in the EMP table, EMPNO is the employee number of the employee
named in the row
- For each row in the EMP table, MGR is the employee number of the employee's
manager
- To obtain the name of the employee's manager, the EMP table must be joined
with itself on the MGR column, comparing that to the EMPNO column, then
locating the ENAME associated with that employee number
SG 3605 8-11
Joining Tables
Non-Equijoins
• Most joins are based on the equality of values in the joined column; therefore they are
called equijoins.
— <>
<=
- >=
Example: List the salary grade, name, salary, and job title of each employee. Order by
grade, and within grade by salary.
8-12 SG 3605
Joining Tables
• Determine the cross product of the tables listed in the FROM clause
• Apply the conditions of the WHERE clause to every row in the cross-product table
Note: If the query has multiple WHERE conditions, they are typically AND'ed
together. A common mistake is to OR the conditions, selecting far too many
rows.
SG 3605 8-13
Joining Tables
Exercise 8-1
Using the functionality you have learned so far, write and execute SQL statements that
will retrieve data from the EMP and DEPT tables to answer the following questions.
(4.' f4A 0
3. List the employees' names and the cities in which they work. Order the list by city.
4,•`'"
4. Retrieve the name and salary of employees who earn more than their managers.
47, 7- a
'tl*:D
, .
5. List the department number, location, and number of employees for every location.
(-1 .f2-4117.:
TA-;;cfr 1..jtV3
O
(--;,(2. • ) )
8-14 SG 3605
9
More Retrieval
Considerations
More Retrieval Considerations
Module 9
More Retrieval Considerations
Materials 9-3
SG 3605 9-1
More Retrieval Considerations
9-2 SG 3605
More Retrieval Considerations
Module Objectives
• Describe and use any of the various programming or processing features that will
help solve retrieval problems, including:
- Reusable cursors
Benefit Statement
This module equips you with skills needed to solve retrieval problems. In addition, many
of the considerations covered will give you insights into how to make your retrieval
programs more efficient.
Note: This modules contains a large number of Unisys extensions to SQL '89. If your
code must be portable, you will avoid using the extensions.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 9-3
More Retrieval Considerations
Indicator Variables
Value Meaning
9-4 SG 3605
More Retrieval Considerations
PROCEDURE DIVISION.
•
ENTER MASM 'ACOB$RDMR' USING
'FETCH all_comp INTO $P1, $P2 $P3, $P4 $P5
ERR-STAT, AUX-INFO,
EMPNO-OUT,
SAL-OUT,
SAL-IND, <
COMM-OUT,
COMM-IND. <
IF COMM-IND < 0 MOVE 0 TO COMM-OUT.
COMPUTE TOTAL-COMP = SAL-OUT + COMM-OUT.
EXEC SQL
FETCH all comp INTO :EMPNO-OUT, :SAL-OUT :SAL-IND,
Z :COMM-IND
:COMM-UT
END-EXEC.
IF COMM-IND < 0 MOVE 0 TO COMM-OUT
COMPUTE TOTAL-COMP = SAL-OUT + COMM-OUT
SG 3605 9-5
More Retrieval Considerations
• ACOB limits the number of parameters on any ENTER MASM call to 31.
• Pass the program variables in the same order as the corresponding host variables in
the SQL command.
Example:
9-6 SG 3605
More Retrieval Considerations
Reusing Cursors
Cursor Review
You may recall that cursors provide a way to deliver rows extracted from a table to your
program. The FETCH command already discussed retrieves one row of the cursor and
correlates the values with your program variables.
The cursor defined in the DECLARE CURSOR command remains in existence until the
end of the thread or until it is dropped. A cursor uses system resources - space in
RDMS's internal tables - so it helps to free this space as soon as possible.
Describe Reusable
Suppose you were writing a program that would first print all the employees in
Department 100, then all the employees in Department 200, and so on. There are two
ways you could do this:
- Declare a separate cursor for each department, and have multiple open cursors in
your program, or
— Declare a single cursor and reuse it, re-evaluating the query for each different
value of DNO
Declaring one cursor saves system resources and is more efficient. RDMS supports
multiple open cursors in a program, but you should save them for the occasions when they
are necessary.
But what if you needed to accept the desired department as input from a user? How is
that information passed to the cursor?
Just as host program variables were used to pass retrieved information back to your
program in a FETCH, they can be used to pass information from your program to an SQL
command, such as OPEN CURSOR.
This capability is possible from ISQL, not ESQL. The ENTER MASM call makes the
correspondence between the placeholder variables and your host program variables.
SG 3605 9-7
More Retrieval Considerations
Figure 9-1 illustrates the typical program flow of a program that reuses cursors.
DECLARE CURSOR
ACCEPT INPUT/
INITIALIZE PROG VAR.
OPEN CURSOR
FETCH
OTHER PROCESSING
END CONDITION?
9-8 SG 3605
More Retrieval Considerations
CLOSE Command
CLOSE cursor-name
• Releases system space associated with the evaluation of the corresponding query with
current parameters
• RDMS automatically closes cursors if you execute a thread control command, END
THREAD for example, or if you encounter a rollback error
Example:
CLOSE HI_LO_SAL
SG 3605 9-9
More Retrieval Considerations
• Unisys SQL extension for the interpreter interface (ISQL), not for ESQL
• Frees the system space used to define the cursor and its associated query
Examples:
9-10 SG 3605
More Retrieval Considerations
• Unisys extension to for ISQL, the interpreter interface, not for ESQL
- $P1 in the DECLARE CURSOR must correspond to the $P1 in the OPEN
- DECLARE may use same variable more than once in WHERE clause
Query in DECLARE CURSOR is evaluated with values from OPEN call to determine
result
Open examples:
The following example illustrates the interpreter interface in a UCS COBOL program.
Recall that UCS COBOL supports both the interpreter interface (ISQL) as well as the
embedded interface (ESQL).
SG 3605 9-11
More Retrieval Considerations
WORKING-STORAGE SECTION.
01 HI-SAL-IN PIC.9(4) VALUE ZEROS.
01 LO-SAL-IN PIC 9(4) VALUE ZEROS.
01 RECORD-RETRIEVED
05 ENAME-OUT PIC X(10) VALUE SPACES.
05 EMPNO-OUT PIC 9(4) VALUE ZEROS.
PROCEDURE DIVISION.
ACCEPT LO-SAL-IN
ACCEPT HI-SAL-IN
MOVE 'OPEN HI_L0 SAL USING $P1, $P2 ;' TO RCOM
ENTER MASM 'ACOB$RDMR' USING RCOM, ERR-STAT,
AUX-INFO, LO-SAL-IN, HI-SAL-IN
9-12 SG 3605
More Retrieval Considerations
Lab 9-1
Reusing Cursors
Write and execute a COBOL program that uses only one cursor to do the following:
Retrieve and display the employee number," name, department number, and commission of
all employees except those in a specified department. If the commission is NULL, display
the word "NONE" in the commission column Accept the department as input and repeat
the display until a dummy department number of 999 is keyed in.
SG 3605 9-13
More Retrieval Considerations
- Opening the cursor sets the pointer to just prior to the first row of the virtual
table.
• Currency allows processing through the cursor using different FETCH commands.
— FETCH FIRST
— FETCH [NEXT]
FETCH LAST
FETCH PRIOR
FETCH CURRENT
EMPNO ENAME
9-14 SG 3605
More Retrieval Considerations
- NEXT is default
- Cannot fetch LAST or PRIOR from a cursor declared with a GROUP BY,
HAVING, or ORDER BY clause
• Retrieves one row from the cursor relative to the current pointer position
• If the cursor is positioned after the last row and you execute a FETCH or FETCH
NEXT, or if you execute a FETCH PRIOR from before the first row of the cursor, an
end-of-cursor status is returned to you.
SG 3605 9-15
More Retrieval Considerations
Unisys SQL extensions allow you to perform random processing on a cursor. The
commands that support this are not part of the ANSI standard SQL. Be aware of this if
you have concerns about the portability of your program.
Suppose you want to process different parts of a cursor separately. For example, from the
EMP table, display first the two rows starting with 5010, then the next two starting with
5784, then three more starting with 5469.
START 1
5010 FOSTER SALESREP
5146 BROWN CLERK
5234 WOODWORTH MANAGER
5237 ROCKWELL ACCOUNTANT
3 5437 MARTIN SALESREP
5469 ADAMS SALESREP
5630 GLASS ACCOUNTANT
5702 TURNER CLERK
5743 LAWSON MANAGER
2 5765 JOHNSON CLERK
5784 WILLIAMS PRESIDENT
5896 SMITH CLERK
5942 FORD SALESREP
5984 HYDE MANAGER
9-16 SG 3605
More Retrieval Considerations
• A RANDOM ACCESS cursor can refer to only one table in the FROM clause
LOCATE Command
LOCATE cursor-name
ON table-name
USING VALUES ( primary-key-value )
• LOCATE uses the index of the primary key to position the currency pointer
SG 3605 9-17
More Retrieval Considerations
• The primary-key-value
FETCH Command
9-18 SG 3605
More Retrieval Considerations
From the EMP table, display first the two rows starting with 5010, then the next two
starting with 5784, then three more starting with 5469.
SG 3605 9-19
More Retrieval Considerations
Lab 9-2
Write the PROCEDURE DIVISION that would read and display the rows of the DEPT
table in reverse DNO order.
9-20 SG 3605
More Retrieval Considerations
- SELECT, FROM, WHERE, GROUP BY, and HAVING clauses are the same as for
the query specification
• Returns one row from a table or tables to a program, typically using the primary key
value to guarantee only one row will be returned
• ISQL/ESQL differences
- For ISQL, if the WHERE clause does not identify a single row, the row retrieved
is the first one found
For ESQL, the singleton SELECT must result in 0 or 1 rows being selected, or
RDMS returns an error
SG 3605 9-21
More Retrieval Considerations
Singleton SELECT:
Replaces:
9-22 SG 3605
More Retrieval Considerations
— If fewer than n rows are fetched, error status = 6001 and SQLCODE = +100
SG 3605 9-23
More Retrieval Considerations
01 REC-RETRIEVED.
05 RR-EMPNO OCCURS 5 PIC 9(5).
05 RR-ENAME OCCURS 5 PIC X(10).
05 RR-JOB OCCURS 5 PIC X(10).
05 RR-MGR OCCURS 5 PIC 9(5).
05 RR-DNO OCCURS 5 PIC 9(4).
05 RR-HIREDATE OCCURS 5 PIC 9(7).
05 RR-SAL OCCURS 5 PIC 9(5).
05 RR-COMM OCCURS 5 PIC 9(5).
01 REC-COMM-IND.
05 RR-COMM-IND OCCURS 5 PIC S9(4)
SIGN LEADING SEPARATE.
EXEC SQL
DECLARE Cl CURSOR SELECT * FROM EMP
END-EXEC.
EXEC SQL
OPEN CURSOR Cl
END-EXEC.
EXEC SQL
FETCH NEXT 5 FROM Cl INTO :RR-EMPNO, :RR-ENAME,
:RR-JOB, :RR-MGR, :RR-DNO, :RR-HIREDATE,
:RR-SAL, :RR-COMM :RR-COMM-IND
END-EXEC.
9-24 SG 3605
More Retrieval Considerations
When programming for better performance, it helps to know how RDMS works and to
take advantage of its features. It is easy to write SQL queries and RDMS will eventually
return the rows requested no matter how inefficiently the query is written. But, there are
steps you can take to make it easier and more efficient to find the data. To be able to
write better queries, you need to know how RDMS locates the data you request.
SG 3605 9-25
More Retrieval Considerations
9-26 SG 3605
More Retrieval Considerations
—> 100 5237 -> 5437 2 5010 ... 5469 ... 5784 ...
—> 100 5630 -> 5765 3 5146 ... 5630 ... 5896 ...
—> 100 5702 -> 5984 5234 ... 5702 ... 5942 ...
—> 100 5743 -> 5237 ... 5743... 5984 ...
200 5010 5437 5765 ...
200 5146
200 5234
200 5437
200 5469
200 5942
300 5784
400 5765
400 5896
400 5984
SG 3605 9-27
More Retrieval Considerations
Secondary Keys
• A secondary key is a set of one or more columns for which RDMS creates an index
that provides fast access to those columns
• If the EMP table were often queried using DNO and ENAME, an index for each
should be declared
9-28 SG 3605
More Retrieval Considerations
• Performance is improved when the columns selected by your query are all part of the
primary or secondary key
Example:
• When multiple secondary indices exist, RDMS uses the one declared first in the
CREATE TABLE command (other factors being equal)
Example:
• If the key is multi-column, the index is used only if the WHERE clause uses the
leading column(s) of the index
SG 3605 9-29
More Retrieval Considerations
Exercise 9-1
Describe the programming or processing features that might help solve the following
retrieval problems.
tj r
t.,C1r
4. Ability to easily retrieve employee records given their social security number.
9-30 SG 3605
More Retrieval Considerations
SG 3605 9-31
10
Data Manipulation
Data Manipulation
Module 10
Data Manipulation
Materials 10-3
SG 3605 10-1
Data Manipulation
Module Objectives
Benefit Statement
You will see in this module how easy it is to modify your database with a few SQL
commands. You probably realize, having said that it is easy to change, that you will need
to deploy safeguards to guarantee the integrity of your database. A few of the integrity
capabilities are touched on here as well, but more on that later.
Note: There are several Unisys extensions for the UPDATE command and one for
the INSERT command. Those syntax extensions are not discussed in this
module, only the SQL 89 formats are presented. If you are maintaining or
migrating SQL code, consult the PRM listed below for the details on the
extensions.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 10-3
Data Manipulation
Data Manipulation
• Data manipulation commands change the data residing in the data tables
— Notify RDMS 1100 that you will update, not just read data
10-4 SG 3605
Data Manipulation
- QUICKLOOKS
- NONE
Examples:
SG 3605 10-5
Data Manipulation
ROLLBACK Command
ROLLBACK WORK
• Before making any changes, it is important to know how to "undo" any modifications
in case of error
• ROLLBACK command discards any changes made to the database since the last
thread control command or rollback
When several updates are to be performed, but only if all of them can be
performed
10-6 SG 3605
Data Manipulation
Introduction to INSERT
Examples. Insert two new departments into the DEPT table as illustrated in Figure 10-1.
SG 3605 10-7
Data Manipulation
INSERT Command
If omitted, the value-list must correspond to the way the table was created, both
by order and number of columns
- Items in the SELECT list must correspond one to one, in order, with the
column-name-list
If the column-name-list is omitted, the items in the SELECT list must correspond
to the way the table was created, both by order and number of columns
10-8 SG 3605
Data Manipulation
Introduction to UPDATE
• As long as primary key and foreign key constraints are obeyed, any data value in any
column or row can be changed.
Note: Both forms of the command rely upon a WHERE clause to restrict the scope of
the update. If you omit the WHERE clause, all rows will be updated!
SG 3605 10-9
Data Manipulation
UPDATE table-specification
SET change-specification
WHERE CURRENT OF cursor-name
• Use to change the content of columns within the current row of the cursor
- Cursor must be declared with the FOR UPDATE OF clause, a Unisys extension
10-10 SG 3605
Data Manipulation
UPDATE table-specification
SET column-change-specification
[ WHERE Boolean-expression ]
• Change the content of columns within a subset of the existing rows of a table
Example: Give all people in Department 200 who do not receive a commission a 7
percent increase in salary and a commission that is 10 percent of their
original salary.
UPDATE EMP
SET SAL = SAL * 1.07,
COMM = SAL * .10
WHERE DNO = 200
AND COMM IS NULL
SG 3605 10-11
Data Manipulation
Requirement: Prompt and read data from a user to update an entire row in the EMP
table. Program is written in ISQL for UCS COBOL.
@UCOB'S DEV.MOD1O/UCOBUPDT,TPF$.UPDT,,,NO-OPTIONS,NARROW
UCOB- 5R2(910816) LSS- 6R1A(910814) 930419 13:37:59
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. update-emp.
3 ENVIRONMENT DIVISION.
4 CONFIGURATION SECTION.
5 SOURCE-COMPUTER. UNISYS-2200.
6 OBJECT-COMPUTER. UNISYS-2200.
7
8 DATA DIVISION.
9 WORKING-STORAGE SECTION.
10
11 01 thread-flag PIC 9 VALUE ZERO.
12 88 thread-has-begun VALUE 1.
13 01 err-end-flag PIC 9 VALUE ZERO.
14 88 no-more-msgs VALUE 1.
15
16 EXEC SQL BEGIN DECLARE SECTION END-EXEC.
17
18 01 update-record.
19 05 up-empno PIC 9(4).
20 05 up-ename PIC X(10).
21 05 up-job PIC X(10).
22 05 up-mgr PIC 9(4).
23 05 up-dno PIC 9(3).
24 05 up-hiredate PIC 9(6).
25 05 up-sal PIC 9(4).
26 05 up-comm PIC 9(4).
27 01 record-out.
28 05 empno-out PIC 9(4).
29 05 PIC X(2) VALUE SPACES.
30 05 ename-out PIC X(10).
31 05 PIC X(2) VALUE SPACES.
32 05 job-out PIC X(10).
33 05 PIC X(2) VALUE SPACES.
34 05 mgr-out PIC 9(4).
35 05 PIC X(2) VALUE SPACES.
36 05 dno-out PIC 9(3).
37 05 PIC X(2) VALUE SPACES.
38 05 hiredate-out PIC 9(6).
39 05 PIC X(2) VALUE SPACES.
40 05 sal-out PIC 9(4).
41 05 PIC X(2) VALUE SPACES.
42 05 comm-out PIC 9(4).
43
10-12 SG 3605
Data Manipulation
SG 3605 10-13
Data Manipulation
95 ***********************************************************
96 • Update row
97 ***********************************************************
98
99 EXEC SQL
100 UPDATE rosyschema2.emp
101 SET empno = :up-empno, ename = :up-ename, job = :up-job,
102 mgr = :up-mgr, dno = :up-dno, hiredate = :up-hiredate,
103 sal = :up-sal, comm = :up-comm
104 WHERE empno = :up-empno
105 END-EXEC.
106
107 DISPLAY 'Update Performed'.
108
109 ***********************************************************
110 • Display updated row
111 ***********************************************************
112
113 EXEC SQL
114 SELECT * INTO :empno-out, :ename-out, :job-out,
115 :mgr-out, :dno-out, :hiredate-out, :sal-out,
116 :comm-out FROM rosyschema2.emp
117 WHERE empno = :up-empno
118 END-EXEC.
119
120 DISPLAY 'The record was changed to:
121 DISPLAY record-out.
122
123 ***********************************************************
124 • End thread
125 ***********************************************************
126
127 400-done.
128
129 END THREAD.
130 MOVE 0 TO thread-flag.
131
132 DISPLAY 'Program has completed'.
133 STOP RUN.
134 **********************************************************
135 ** Do the RDMS error handling - display codes & messages
136 **********************************************************
137
138 *** Come here when RDMS errors are detected
139
140 900-rdms-error.
141
142 *** Turn off error handling, print error status variables
143
144 EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
145
146 DISPLAY '***SQL Code = ' SQLCODE.
147 DISPLAY '***Error Code = ' ERROR-STATUS.
148 DISPLAY '***Auxiliary Info = ' AUX-INFO.
149
150 *** Issue the error messages, end the thread, and exit
151
152 MOVE 0 TO err-end-flag.
153 PERFORM 940-geterror UNTIL no-more-msgs.
154 DISPLAY 'Error termination'.
155 END THREAD.
156 STOP RUN.
10-14 SG 3605
Data Manipulation
157
158 *** Fill the error buffers using GETERROR
159
160 940-geterror.
161 GETERROR INTO :err-s(1), :err-s(2), :err-s(3), :err-s(4).
162 PERFORM 980-print-error
163 VARYING err-s-index FROM 1 BY 1 UNTIL err-s-index > 4.
164
165 *** Display the error buffers
166
167 980-print-error.
168 IF err-s(err-s-index) NOT = SPACE
169 DISPLAY err-s(err-s-index)
170 ELSE
171 MOVE 5 to err-s-index
172 MOVE 1 TO err-end-flag.
SIZES: CODE(EM4): 624 DATA: 1674
END UCOB- 0 ERRORS(MAJOR) 0 ERRORS(MINOR)
@XQT
Enter the current 4 digit employee number
Enter employee last name
Enter job title
Enter employee number of manager
Enter department number
Enter hiredate
Enter employee monthly salary
Enter commission, or NULL if not applicable
Update Performed
The record was changed to:
5469 ADAMS SALESREP 5234 200 870326 4000 1000
SG 3605 10-15
Data Manipulation
DELETE Command
CURRENT OF deletes the row where the cursor pointer is currently positioned
— Table names must match for the DECLARE and DELETE commands
— Cursor does not need to be declared with the FOR UPDATE OF clause
• RUMS returns the number of rows deleted through the auxiliary information
parameter
10-16 SG 3605
Data Manipulation
DELETE Examples
SG 3605 10-17
Data Manipulation
Requirement: Prompt and read an employee number from a user and delete that row.
Continue prompting and deleting rows until a dummy value of 9999 is
read.
@ACOB,SE DEV.ACOBDELT,TPFS.REL
ACOB 7R1A R 75R3n 03/05/93 18:15:43 (0) 1100 ASCII COBOL 08/12/91 10:02
1100 ASCII COBOL SOURCE LISTING
1
2 IDENTIFICATION DIVISION.
3 PROGRAM-ID. TERMINATOR.
4 ENVIRONMENT DIVISION.
5 CONFIGURATION SECTION.
6 SOURCE-COMPUTER. UNISYS-2200.
7 OBJECT-COMPUTER. UNISYS-2200.
8
9 DATA DIVISION.
10 WORKING-STORAGE SECTION.
11 01 RCOM.
12 05 RLIN PIC X(40) OCCURS 12 TIMES.
13 01 ERR-STAT PIC 9(4).
14 01 AUX-INFO PIC S1(36).
15 01 ERROR-TEXT.
16 05 ERR-S PIC X(132) OCCURS 4 TIMES.
17 01 THREAD-FLAG PIC 9 VALUE ZERO.
18 88 THREAD-HAS-BEGUN VALUE 1.
19 01 D-EMPNO PIC 9(4).
20 *
21 PROCEDURE DIVISION.
22 ***************************************************************
23 Main Control Paragraph
24 ***************************************************************
25 000-MAIN.
26 PERFORM 200-INIT THRU 299-INIT-EX.
27 PERFORM 400-LOOP THRU 499-LOOP-EX
28 UNTIL D-EMPNO = 9999.
29 PERFORM 600 FIN THRU 699-FIN-EX.
30 STOP RUN.
31 ***************************************************************
32 Begin thread, read an employee number
33 ***************************************************************
34 200-INIT.
35 MOVE 'BEGIN THREAD FOR UDSSRC ' TO RCOM.
36 MOVE ' UPDATE (DEFERRED) • ' TO RLIN(2).
37 ENTER MASH 'acob$rdmr' USING RCOM, ERR-STAT, AUX-INFO.
38 IF ERR-STAT NOT = 0 GO TO 900-ERROR-PRINT.
39 MOVE SPACES TO RCOM.
40 MOVE 1 TO THREAD-FLAG.
41 DISPLAY 'Enter 4 digit employee number' UPON PRINTER.
42 ACCEPT D-EMPNO.
43 299-INIT-EX.
44 EXIT.
10-18 SG 3605
Data Manipulation
45 **************************************************************
46 * Delete a row, solicit another
47 **************************************************************
48 400-LOOP.
49 MOVE 'DELETE FROM ROSVSCBEMA2.EMP ' TO RCOM.
50 MOVE 'WHERE EMPNO = $P1 ;' TO RLIN(2).
51 ENTER MASM 'ACOB$RDMR' USING RCOM, ERR-STAT,
52 AUX-INFO, D-EMPNO.
53 IF ERR-STAT NOT = 0 GO TO 900-ERROR-PRINT.
54 DISPLAY 'Record deleted' UPON PRINTER.
55 DISPLAY 'Enter 4 digit employee number' UPON PRINTER.
56 ACCEPT D-ENPNO.
57 499-LOOP-EX.
58 EXIT.
59 **************************************************************
60 • End the thread, issue all done
61 **************************************************************
62 600-FIN.
63 MOVE 'END THREAD;' TO RCOM.
64 ENTER MASK 'ACOB$RDMR' USING RCOM, ERR-STAT, AUX-INFO.
65 IF ERR-STAT NOT = 0000 PERFORM 900-ERROR-PRINT.
66 DISPLAY 'ALL DONE.' UPON PRINTER.
67 MOVE SPACES TO RCOM.
68 699-FIN-EX.
69 EXIT.
70 **************************************************************
71 • Print the error message, end the thread, shut it down *
72 **************************************************************
73 900-ERROR-PRINT.
74 DISPLAY 'RDML COMMAND = ' RCOM.
75 DISPLAY 'ERROR STATUS = ' ERR-STAT.
76 DISPLAY 'ERROR COLUMN = ' ADX-INFO.
77
78 MOVE 'GETERROR INTO $P1, $P2, $P3, $P4 ;' TO RCOM.
79 ENTER MASM 'ACOB$RDMR' USING RCOM, ERR-STAT, AUX-INFO,
80 ERR-S(1), ERR-S(2), ERR-S(3), ERR-S(4).
81 DISPLAY ERR-S(1) UPON PRINTER.
82 DISPLAY ERR-S(2) UPON PRINTER.
83 DISPLAY ERR-S(3) UPON PRINTER.
84 DISPLAY ERR-S(4) UPON PRINTER.
85
86 IF TBNEAD-BAS-BEGUN
87 MOVE 'END THREAD ;' TO RCOM
88 ENTER MASM 'ACOB$RDMR' USING RCOM,
89 ERR-STAT, ADX-INFO.
90
91 DISPLAY 'ERROR END' UPON PRINTER.
92 STOP RUN.
END ACOB DIAGNOSTIC TOTALS 0 WARNING 0 MINOR 0 SERIOUS 0
FATAL 0 LEVELING
COMPILE TIME IS 4 33 SECONDS CORE: MIN= 63601 / 64000 MAX* 66560 WORDS
SG 3605 10-19
Data Manipulation
@MAP,S ,TPF$.ABS
Collector 33R1A (910808 1521:53) 1993 Mar 05 Fri 1815:44
1. NOT TPF$.
2. IN TPFS.REL
3. IN SYS$LIBS*FtSA.CBEP$$RSA
4. IN SYS$LIB$*RSA.RDI/R-ACOBDAT
5. END
AFCM status of output element is SETAFCM
Quarter-word sensitive
ADDRESS LIMITS 001000 001726 471 IBANK WORDS DECIMAL
040000 061623 9108 DBANK WORDS DECIMAL
STARTING ADDRESS 001336
10-20 SG 3605
Data Manipulation
SG 3605 10-21
Data Manipulation
Exercise 10-1
1. Change the salary for the employee with EMPNO = 5146 to 1250.
flo
10-22 SG 3605
Data Manipulation
Lab 10-1
Write and execute a COBOL program that reads values for a row of the DEPT table and
insert the row into the table. Have the program display the inserted row.
Extension: Continue to read and insert rows until a dummy value of 999 is entered for
DNO.
SG 3605 10-23
11
Keys and Updating
Keys and Updating
Module 11
Keys and Updating
Materials 11-3
SG 3605 11-1
Keys and Updating
Module Objectives
Benefit Statement
How can you maintain consistency between tables? This is the main question answered in
this module. And once you establish the mechanisms to maintain consistency, how do you
update your database?
Materials
SG 3605 11-3
Keys and Updating
Primary Keys
11-4 SG 3605
Keys and Updating
Update Anomalies
• What would happen to the EMP table below if you executed the following updates?
UPDATE DEPT
SET DNO = 500
WHERE DNO = 200
SG 3605 11-5
Keys and Updating
Foreign Keys
Introduction
11-6 SG 3605
Keys and Updating
PARENT
DEPT DNO DNAME LOC
CHILD
EMP
• If foreign key relationship is added after tables have data, RDMS does not check
retroactively if data satisfy integrity constraints
SG 3605 11-7
Keys and Updating
- Parent table must have a row with the primary key value corresponding to the
foreign key
• Sample problem
— If foreign key DNO is defined as a foreign key, as shown in Figure 11-2, and the
following INSERT is executed:
• Problem solution: Add the corresponding department to the DEPT table, then insert
the row in EMP
11-8 SG 3605
Keys and Updating
- There must be no rows in child table having primary key value from parent as
the foreign key
- Change rows in child table so foreign key is a valid primary key value
• Sample problem
— Generates an error message since all the rows in EMP having DNO = 200 will be
stranded
• Solution to problem
SG 3605 11-9
Keys and Updating
There must be no rows in child table having old primary key value from parent
as the foreign key
- Change rows in hild table so foreign key is a valid primary key value
• Sample Problem: The following UPDATE generates an error message since all the
rows in EMP having DNO = 200 will be stranded
UPDATE DEPT
SET DNO = 500
WHERE DNO = 200
— May be changed to any other primary key value from parent table
UPDATE EMP
SET DNO = 400
WHERE EMPNO = 5146
11-10 SG 3605
Keys and Updating
SG 3605 11-11
Keys and Updating
Lab 11-1
The tables you have been working with have a foreign key. The column DNO in EMP is
declared as a foreign key referring to DNO in DEPT. You will not use these tables again
after this exercise. You may commit or rollback the changes you make.
2. Perform the correct procedure for inserting the same row as above into EMP.
3. Try to delete a row directly from DEPT whose primary key is associated with one
or more rows in EMP,' '
5. Try to update a row in EMP, changing the DNO to a currently non-existent value.
6. Update a row as above, changing DNO from one its current value to another valid
value.
7. Try to change a DNO in DEPT which has a corresponding foreign key value in
EMP.
11-12 SG 3605
12
Obtaining Table
Definition Information
Obtaining Table Definition Information
Module 12
Obtaining Table Definition Information
Materials 12-3
SG 3605 12-1
Obtaining Table Definition Information
Module Objectives
Using UREP
Benefit Statement
You will need to obtain table information to discover the data types and sizes for the
columns in tables. This information is needed for writing host program variables. The
table information provided by UREP will also tell you about primary, secondary, and
foreign keys that exist for your tables.
Materials
SG 3605 12-3
Obtaining Table Definition Information
How to obtain table column information using the MAPPER RDI interface.
Step 1. On the RDI Menu, transmit from the Select entry as shown in Figure 12-1.
RDI
RDI menu
Tab to your choice and press XMIT
Select
Join
Insert ►
Delete ►
Update ►
Create ►
SQL ►
Utilities menu
Detailed help ►
Rev. Level 3R3
Step 2. On the Select menu, enter the table name and a question mark in the Column
names field. Notice that the table name is qualified by the name ROSVSCREMAL
RN
Select menu
Retrieve rows from a single relational table.
Table name rosyschemai.emp
Column names -> ?
Where clause ->
Retrieve all rows? -> y If no, how many? -> 1
Number of rows to skip -> Null Symbol -> *
Extra options? -> n
Place cursor here and press XMIT ->
Press XMIT from here for detailed help ->
12-4 SG 3605
Obtaining Table Definition Information
Step 3. The RESULT displayed contains the column information for the EMP table, as
shown in Figure 12-3.
Column number
Column name
SG 3605 12-5
Obtaining Table Definition Information
Introduction
• Benefits
— Locate information
- Identify how information is used
• Reports are available to provide information on how the tables and views are defined
12-6 SG 3605
Obtaining Table Definition Information
@DD,option source-input-element,source-output-element,application-group
E echoing the incoming processor commands; they are not printed otherwise
D demand execution
B batch execution
• Commands are written in the Data Dictionary System (DDS) command language
SG 3605 12-7
Obtaining Table Definition Information
UREP Example
The following example illustrates the commands to obtain table information for the EMP
and DEPT tables.
@DD,E
UREP 1R2 (08/16/91 11:09:26) 03/26/93 15:48:14
*REMARK* UREP7403 The default UDS application ( UDSSRC ) will be used.
1 process table emp for schema rosyschemal report
** Report for Table EMP for Schema ROSVSCHEMA1 ***
Logical Filename: EMPLOYEE for Schema ROSVSCHEMA1
Preamble Length : 14
Rdt Length : 52
Total Length : 66
Owner Userid :
Data Access Control : ACTIVE
Base Relation Code : 1
Record Size(words) : 13
Number of Non-deleted Items : 8
Last Non-deleted Item : 8
Number of Foreign Keys : 0
Number of Member Relations : 0
Loading Order : RANDOM
Item Mod Flag : FALSE
Current Number Indexes : 0
Matching Order Flag : TRUE
Screen Type : COLUMNAR
Columns : Scale /
Name Code Type Size Precision
Nulls
EMPNO 1 DECIMAL 5 0
NOT-ALLOWED
ENAME 2 CHARACTER 10 0
NOT-ALLOWED
JOB 3 CHARACTER 10 0
ALLOWED
MGR 4 DECIMAL 0
ALLOWED
DNO 5 DECIMAL 4 0
ALLOWED
HIREDATE 6 DECIMAL 7 0
ALLOWED
SAL 7 DECIMAL 5 0
ALLOWED
COMM 8 DECIMAL 5 0
ALLOWED
Primary Key : ENO
Column, Direction
EMPNO, ASCENDING
12-8 SG 3605
Obtaining Table Definition Information
SG 3605 12-9
Obtaining Table Definition Information
Lab 12-1
Use the MAPPER MRI interface or UREP directly to obtain table information for the
EMP table you are working with. How does the information received compare to the
information contained in this module?
12-10 SG 3605
13
Views
Views
Module 13
Views
Materials 13-3
Views 13-4
Basic Definition 13-4
What is a View? 13-5
Purpose of Views 13-6
SG 3605 13-1
Views
Module Objectives
- Drop views
Benefit Statement
By working with views you will be able to solve several database problems. You will see
how to limit the information that is viewed or that can be updated by users. You also will
be able to safeguard your table from updates with "unrealistic" values.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 13-3
Views
Views
Basic Definition
A view is a window into the data. Views provide multiple perspectives of the same
underlying tables.
Figure 13-1 shows three columns and a few rows that comprise the EMPMGR view.
EMPMGR View
EMPNO ENAME JOB
EMP Table
5010 FOSTER SALESREPT
5146 BROWN CLERK MGR DNO HIREDATE SAL COMM
5234 WOODWORTH MANAGER
5237 ROCKWELL ACCOUNTANT 5234 200 860714 2700 550
5234 200 871011 1000
5784 200 790219 3250
5743 100 840618 2175
5437 MARTIN SALESREP 5234 200 870326 2500 300
5469 ADAMS SALESREP 523 4 200 811102 2250 500
5630 GLASS ACCOUNTANT 5743 100 871220 1850
5702 TURNER CLERK 5743 100 890515 900
5743 LAWSON MANAGER 5784 100 830510 3400
5765 JOHNSON CLERK 5984 400 891221 975
5784 WILLIAMS PRESIDENT 300 781015 5200
5896 SMITH CLERK 5984 400 880913 1050
5942 FORD SALESREP 5234 200 890316 2375 0
5986 HYDE MANAGER 5784 400 850901 2900
13-4 SG 3605
Views
What is a View?
• A view is:
SG 3605 13-5
Views
Purpose of Views
— Pre-selects columns
— Joins tables
13-6 SG 3605
Views
• Unisys supports the SQL 89 form of this commmand; the WITH ACCESS CONTROL
clause is a Unisys extension to the standard command
• Owner of a view is the user-id that created it; you cannot transfer ownership
- GROUP BY clause
HAVING clause
SG 3605 13-7
Views
View Example
--Name--- -Location-
FOSTER NEW YORK
BROWN NEW YORK
WOODWORTH NEW YORK
ROCKWELL ATLANTA
MARTIN NEW YORK
ADAMS NEW YORK
GLASS ATLANTA
TURNER PRINCETON
LAWSON ATLANTA
JOHNSON PRINCETON
WILLIAMS DETROIT
SMITH PRINCETON
FORD NEW YORK
HYDE PRINCETON
-Location-
PRINCETON
ATLANTA
NEW YORK
DETROIT
13-8 SG 3605
Views
Columns in Views
- Functions
Arithmetic expressions
- Constants
- Constants
Example:
SG 3605 13-9
Views
Data Checking
• UPDATE and INSERT commands have values checked against WHERE clause of the
definition of the view
Example:
Security
• Owner can use GRANT and REVOKE commands for privileges on view
Example:
13-10 SG 3605
Views
Updating Views
• All changes to a view are reflected in the base table and vice versa
View is read-only if the CREATE VIEW command includes any of the following
- DISTINCT keyword
- A column specification that appears more than once in the select list
- A GROUP BY clause
- A HAVING clause
SG 3605 13-11
Views
— Access control is activated for a different owner for an underlying base table
Example:
13-12 SG 3605
Views
Exercise 13-1
Views
1. What is a view?
SG 3605 13-13
Views
Lab 13-1
1. Create a view based on EMP and DEPT, and another view based on EMP alone.
Incorporate renaming some columns and use of functions and/or arithmetic
expressions.
- Start your commands with a BEGIN THREAD command and end with an
END THREAD command.
3. Attempt to delete a row from the first view; from the second view.
13-14 SG 3605
14
More About Threads
and UDS
More About Threads and UDS
Module 14
More About Threads and UDS
Module Objectives 14-3
Materials 14-3
Threads 14-4
Locking 14-14
Concurrent Access 14-14
Locking 14-14
Types of Locks 14-15
Deadlock 14-19
SG 3605 14-1
More About Threads and UDS
Module Objectives
Benefit Statement
Two large issues related to database integrity center around disruption of the computer
itself and multiple users accessing the data. In this module you will realize the choices
you have at your disposal in regard to these issues, as well as the automatic coverage
provided by UDS Control and the Integrated Recovery Utility.
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) and IPF SQL
Interface End Use Guide
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 14-3
More About Threads and UDS
Threads
Thread control
- Application groups
Recovery options
- Concurrency control
• Thread control commands are processed directly by UDS Control, without invoking
RDMS
14-4 SG 3605
More About Threads and UDS
< 0 0 0 0 >
SG 3605 14-5
More About Threads and UDS
BEGIN THREAD
• COMMIT WORK
— Ends a step
• ROLLBACK
— Ends a step
• END THREAD
- Program must BEGIN THREAD again before issuing more SQL commands
14-6 SG 3605
More About Threads and UDS
SG 3605 14-7
More About Threads and UDS
• Unisys implementation
COMMANDLOOKS
- DEFERRED
- QUICKLOOKS
- NONE
Example
14-8 SG 3605
More About Threads and UDS
Application Groups
- Each has its own common banks, database files, audit files, repository, and other
support components
• ACOB program with SQL must be collected with proper application group elements
• UCOB program with SQL must be linked to proper application group elements
Example: Compile and execute a UCOB ESQL program that will use application group
seven
@USE LINK$PF,SYS$LIB$*APP$7.
@XQT tpf$.progl
SG 3605 14-9
More About Threads and UDS
Recovery Options
• QUICKLOOKS
• DEFERRED
• COIVIMANDLOOKS
• NONE
14-10 SG 3605
More About Threads and UDS
COMMIT Command
COMMIT WORK
Example:
COMMIT WORK
SG 3605 14-11
More About Threads and UDS
— ADVANCE
— TERMINATE
• ADVANCE
• TERMINATE
14-12 SG 3605
More About Threads and UDS
• SQL extension
Note: Unless you are using TIP with MCB, it may not be relevant to specify this
option.
• Not specified
• KEEP
• DISCARD
• RE QUEUE
SG 3605 14-13
More About Threads and UDS
Locking
Concurrent Access
In a normal database environment, many users will access the same table, maybe even
the same rows, simultaneously. If everyone is just looking at the data, there is no
problem with concurrent access. The problem occurs when two users want to update, or
one user wants to update and another look at the same information. In this case, the
database management system software must control who can read and who can update
the data.
To ensure that no user is looking at information that is currently being changed, RDMS
1100 places contention locks on the data. Two rules apply:
Locking
UDS Control and RDMS 1100 provide a system of locking that gives basic protections to
users. You can let RDMS automatically do the locking for you. This is implicit locking.
For more efficient processing and specific locking, you can incorporate LOCK commands
explicitly in a program.
14-14 SG 3605
More About Threads and UDS
Types of Locks
• RETRIEVAL (R)
• UPDATE (U)
• SHARED (S)
- Prevents others from viewing updated but not yet committed data
• PROTECTED (P)
• EXCLUSIVE (X)
- Strongest lock
• Eight types of locks are allowed by RDMS: R, SR, U, SU, PR, PU, XR, and XU
SG 3605 14-15
More About Threads and UDS
LOCK Command
• Unisys implementation
- Some tables could be locked or others not, or queuing could take place for
some but not all tables
— RETURN means RDMS issues an error and control returns to your program
- , ,
— QUEUE means I MS queues your program until the locking conflict is resolved
• Usage guidelines
Examples:
14-16 SG 3605
More About Threads and UDS
RDMS implicitly locks rows of a table as they are read or updated. The type and duration
of implicit lock depends on the type of explicit table-level lock used. Table 14-1 shows the
implicit locks set by RDMS.
For a cursor declared with the FOR UPDATE clause, RDMS places a retrieval
lock on each row as it is fetched, releasing the lock only when you alter the
currency of the cursor by fetching a different row from the cursor, closing the
cursor, dropping the cursor, or ending the step.
SG 3605 14-17
More About Threads and UDS
Locking Conflicts
A locking conflict occurs if two users try to lock the same table at the same time. Table
14-2 summarizes the conditions in which locks from different runs conflict. An * indicates
a conflict.
Conflicts apply to both table and row level locks, but table-level locks can only conflict
with table-level locks, and row-level locks with row-level locks
Current Lock -3 R SR U SU PR PU XR XU
New Usage i
Retrieval * *
Shared Retrieval * *
Update * * * *
Shared Update * * * *
Protected Retrieval * * * * *
Protected Update * * * * * *
Exclusive Retrieval * * * * * * * *
Exclusive Update * * * * * * * *
L D cr-
14-18 SG 3605
More About Threads and UDS
Deadlock
Deadlock occurs when two or more runs are queued on locks held by each other so that
none of the runs can resume execution.
Table 1
Table 2
---).Lock held
Lock attempted
SG 3605 14-19
More About Threads and UDS
Resolving Deadlock
— ERR-STAT = 6010
— Test in program
• Suggested resolution
Repeat all OPEN, INSERT, DELETE, and UPDATE commands in step rolled
back
- Include in loop
- Use randomly generated wait times if several threads are doing this
'-;„ • " • -
• On conflict, the RETURN option of the LOCK command returns:
14-20 SG 3605
More About Threads and UDS
UNLOCK Command
UNLOCK table-specification-list
• Unisys implementation
— COMMIT
- END THREAD
Example:
SG 3605 14-21
More About Threads and UDS
BEGIN THREAD
DECLARE CURSOR
LOCK
DROP CURSOR
V
UNLOCK
END THREAD
14-22 SG 3605
More About Threads and UDS
Exercise 14-1
!-•
SG 3605 14-23
15
Getting Physical -
Tables and Storage
Areas
Getting Physical - Tables and Storage Areas
Module 15
Getting Physical - Tables and Storage Areas
Materials 15-3
Schema 15-5
Storage-Areas 15-6
SG 3605 15-1
Getting Physical - Tables and Storage Areas
Module Objectives
Benefit Statement
Access and manipulating tables have been the main topic for this course, but what if you
are responsible for setting up the database. A few insights into creating the tables and
how they relate to the database physical attributes are presented here. Your next step
may be database administration!
Materials
• OS 1100 UDS Relational Data Management System (UDS RDMS 1100) SQL
Programming Reference Manual
SG 3605 15-3
Getting Physical - Tables and Storage Areas
Creating a Database
15-4 SG 3605
Getting Physical - Tables and Storage Areas
Schema
• Definition
Database definition that describes units of data, such as record types and
relations
A name that identifies an arbitrary group of storage area definitions and table
definitions in repository
Schema
Storage-Area Table
Descriptions Descriptions
SG 3605 15-5
Getting Physical - Tables and Storage Areas
Storage-Areas
• Definition contains:
Storage-Area Name
— Version-name
15-6 SG 3605
Getting Physical - Tables and Storage Areas
Create
Storage-Area
Company.Emp:Prod
Company.Emp:Tpst
Associate
with File
Create Table
and Enter Data
Company.Emp:Prod
Compeny.Emp:Test
SG 3605 15-7
Getting Physical - Tables and Storage Areas
- Primary key(s)
- Secondary key(s)
- Foreign key(s)
Pages allocated
15-8 SG 3605
Getting Physical - Tables and Storage Areas
Table Versions
• Versions of the table are created when different data is loaded in the specified table
versions
• Versions can be used to divide a large table into tables with fewer rows
SG 3605 15-9
Getting Physical - Tables and Storage Areas
schema-name. table-name:
• Default version
• Fully qualified table name may be used anywhere table name must be coded
• Can set up own defaults for qualifier/version so only table name is needed
Examples:
Company. Payroll:Production
Company.Payroll:Test
15-10 SG 3605
Getting Physical - Tables and Storage Areas
• Unisys implementation
- Schema-name
— Name or string
Generalized programs
Examples:
. SG 3605 15-11
Getting Physical - Tables and Storage Areas
Payroll
RDMS.PAYROLL•PRODUCTION
Items in RDT 2
vir
Table name
Qualifier
Storage-Area name
Column names
Primary key
Foreign key
Indexes
Items in FDT
V
Storage-Area name
Schema name
Version name
File name
Other attributes
Mass-storage file IL 4
Data associated
with this
storage-area
c_
15-12 SG 3605
Getting Physical - Tables and Storage Areas
1. When you give RDMS a table name, it must first determine the full form of the name in
the format schema.table:version.
If you do not specify the schema or version, RDMS uses the current default values.
Unless a USE command was entered, the default schema is RDMS, and the default
version is PRODUCTION.
2. RDMS uses the schema and table names together to find the encoded form of the table
description (Relational Table Description or RDT) in the RDT$FILE.
3. RDMS gets the storage-area name from the RDT, and combines it with the version name
to identify the File Description Table (FDT) in FDT$FILE.
The FDT is the encoded form of the Storage-Area Definition.
This contains the name of the file in which the table data is physically stored.
4. RDMS gets the EXEC file name or TIP file number from the FDT. It then goes to the
file and locates the table.
SG 3605 15-13
Getting Physical - Tables and Storage Areas
@DD,E
CREATE SCHEMA MASTER INSTRUCTOR.
CREATE STORAGE-AREA INSTRUCTOR
VERSION KING_ OF PRUSSIA
FOR SCHEMA MASTER_ INSTRUCTOR.
ADD QUALIFIER KOP. -
ADD FILENAME INSTRUCTORS.
ADD FILE-TYPE EXEC.
PROCESS STORAGE-AREA INSTRUCTOR
VERSION KING_ OF PRUSSIA
FOR SCHEMA MASTER INSTRUCTOR INSTALL.
CREATE STORAGE-AREA INSTRUCTOR
VERSION NEW YORK CITY
FOR SCHEMA MASTER_ INSTRUCTOR.
ADD QUALIFIER NYC.
ADD FILENAME INSTRUCTORS.
ADD FILE-TYPE EXEC.
PROCESS STORAGE-AREA INSTRUCTOR
VERSION NEW YORK CITY
FOR SCHEMA E
RAST INSTRUCTOR INSTALL.
EXIT.
15-14 SG 3605
Getting Physical - Tables and Storage Areas
Exercise 15-1
3. Write the commands needed to make the table-name EMP resolve to US.EMP:DIV1.
(`j
SG 3605 15-15
A
System Usage
Information
System Usage Information
Appendix A
System Usage Information
SG 3605 A-1
System Usage Information
System Essentials
Your instructor will assist you in filling out the information needed for this class.
• Demand Login
— $$open-ID
— User-ID/Password
— Run-ID
— Account
— Project-ID
• MAPPER Login
$$OPEN-ID
— MAPPER-ID
- User-ID
Password
— Department-#
• Printer information
Printer-ID
Printer location
Other
• Database information
- Qualifier/Schema-name
- Table Names
- Application group
SG 3605 A-3
System Usage Information
You may need to note changes required by your site for the following "typical" session.
Note: The greater than sign ">" is used instead of the standard start of entry (SOE)
character below.
>$$OPEN DMEA10
>USER-ID/PASSWORD
>Project-ID: STU . System either solicits project... or
>@RUN run-ID,account,stu ... or requires run statement
Note: You will need a couple of files for this class, a program file for your programs
and a data file for your printout. The suggested names of the files will both
have your STU number as the qualifier. The program file should be named
SQL and the printfile named PRFIL. These are the names you will find in the
provided runstreams. Change them if you wish, but don't step on someone
else's files!
Note: The above command lists the elements provided for this class. When an
element name has two parts divided by the "/", the first part tells you where
this element is used and the second part tells you what it is used for. For
example, the element LAB3-1 I UCOBSKEL is the element for Lab 3-1 and is a
UCS COBOL skeleton. Other elements will be described by your instructor.
A-4 SG 3605
System Usage Information
You may need to note changes required by your site for the following "typical" session.
>$$OPEN T4EA10
>EDMAP . Select a specific MAPPER
Enter userid,dept#,password on Menu or on the control line
after ")"
>RDI . Entered from control line
SG 3605 A-5
System Usage Information
Obtaining Printout
Note: This information is site dependent. You may have an on-site printer, a local
attached printer, a remote printer, or no printer available! And how you access
the printer will require specific instructions. Please make your notes below.
Printer Notes
A-6 SG 3605
System Usage Information
• Login to IPF
>@IPF filename
>-C-->old Qual*filename.elt
• Set datamanager
>-C->Set $data := rdms
• Run program
›-C->run filename.elt, compiler, library-list
>-C-->run sql.lab3-1/ucobskel, ucob
— Defaults
— System library
>-C-->run ,ucob
Compiles and executes work space using ucob compiler and system library
SG 3605 A-7
System Usage Information
Use Runstreams
Note: Runstreams are provided in your student file to automate this process. Please
modify them for your use during this class. If the version name of the element
contains "r-u-n" it is likely to be a runstream. Example of using one:
>@ADD,L sql.lab3-1/runucobskel
ACOB program
The following pieces are intended to operate together in a demand session.
• Execute absolute
UCOB program
• Compile source program
A-8 SG 3605
System Usage Information
Using and modifying the provided runstreams automates the process of using the
standard ECL. You can save your printfile information in your program file with an ECL
copy:
>@copy,i prfil.,sql.lab3-1/ranucobskel . Note the "ran" versus
"run" name
A popular, but unsupported, pair of processors can be found at most sites: @BK1 and
@BK2.
Example 1
>@BK1
>@UCOB,res sql.lab3-1/ucobskel,om
>@BK2,I
Example 2
>@BK1
>@ACOB,se sql.lab3-1/acobskel,rel
>@EOF
>@ADD sql.map
>@BK2 prinl
SG 3605 A-9
System Usage Information
A-10 SG 3605
B
FORTRAN Examples
FORTRAN Examples
Appendix B
FORTRAN Examples
SG 3605 B-1
FORTRAN Examples
Format 1
CALL F$RDMR
('SQL-COMMAND' ,
ERSTAT ,
AUXINF )
Format 2
CALL F$RDMR
(SQL-COM-STRING-VARIABLE ,
ERSTAT
AUXINF )
Example
CHARACTER*132 RCOM
CHARACTER*4 ERSTAT
INTEGER AUXINF
CALL F$RDMR
* ( 'BEGIN THREAD FOR UDSSRC READ
ERSTAT, AUXINF )
SG 3605 B-3
FORTRAN Examples
(At this stage, you don't need to be concerned about the syntax and layout of the
commands. This is explained in the modules. For now, look at the way the RDMS
commands are embedded in the 'CALL F$R.DMR...' commands.)
Requirement: Read and display all the rows of the EMP table, showing all the columns
C
c****************************************************************
C * MAIN PROGRAM TO PRINT CONTENTS OF THE "SALESMEN" TABLE
c****************************************************************
CHARACTER*511 rcom
CHARACTER*4 erstat
INTEGER auxinf
INTEGER rempno
CHARACTER*10 rename
CHARACTER*10 rjob
INTEGER rmgr
INTEGER rdno
INTEGER rhirdt
INTEGER rsal
INTEGER rcomm
C
c****************************************************************
C * CONNECT TO UDS
*****************************************************************
RCOM = 'BEGIN THREAD FOR UDSSRC READ UDSMSG ;'
CALL F$RDMR(RCOM, ERSTAT, AUXINF)
CALL errchk(erstat, auxinf, ", *90, *90)
C
C
c****************************************************************
C DECLARE THE CURSOR
c****************************************************************
PRINT '('Retrieves entire relation EMP', ///)'
RCOM = 'DECLARE EMPLOYEE CURSOR SELECT * FROM EMP;'
CALL F$RDMR(RCOM, ERSTAT, AUXINF)
CALL errchk(erstat, auxinf, ' ', *90, *90)
B-4 SG 3605
FORTRAN Examples
C
C****************************************************************
C LOOP TO READ TABLE AND PRINT ROW *
C****************************************************************
10 CONTINUE
RCOM = 'FETCH NEXT EMPLOYEE INTO $P1, $P2, $P3, $P4, $P5,
* $P6,$P7,$P8 ;'
CALL F$RDMR(RCOM, ERSTAT, AUXINF,
* REMPNO, RENAME, RJOB, RMGR, RDNO, RHIRDT, RSAL, RCOMM)
CALL errchk(erstat, auxinf, '6001', *90, *80)
C
PRINT ' (1X, I4,1X,A, I3,1X, I6, 1X, I4,1X, I4) ' ,
* REMPNO, RENAME, RJOB, RMGR, RDNO, RHIRDT, RSAL, RCOMM)
GO TO 10
C
C
80 CONTINUE
PRINT 'End of relation EMP.'
CALL F$RDMR('END THREAD;', ERSTAT, AUXINF)
CALL errchk(erstat, auxinf, ", *90, *90)
PRINT '(23HONormal end of program.)'
STOP
C
90 CONTINUE
PRINT '(40H0***** Error status program termination.)'
CALL F$RDMR('END THREAD;', ERSTAT, AUXINF)
STOP
END
C
C*
C SUBROUTINE TO TEST ERROR STATUS
C
C
SUBROUTINE errchk (erstat, auxinf, accept, *, *)
C
CHARACTER*4 accept @ Acceptable status for RETURN 2
INTEGER auxinf
CHARACTER*4 erstat
C
IF (erstat .EQ. '0000') RETURN
IF (erstat .EQ. accept) RETURN 2
C
PRINT '(29H0**** Unexpected error status ,A,11H
at column,I3)', erstat, auxinf
RETURN 1
SG 3605 B-5
FORTRAN Examples
PRINT "(A132)', El
PRINT '(A132)', E2
PRINT '(A132)', E3
PRINT '(A132)', E4
PRINT '(A132)', E5
B-6 SG 3605
C
ACOB Skeleton
ACOB Skeleton
@PRT,S AL3644*SQL.LAB3-1/RUNACOB
FURPUR 30R2A (910822 1304:40) 1993 Mar 17 Wed 1510:31
AL3644*SQL(1).LAB3-1/RUNACOB(0)
•1 @BRNPT PRINT$,prfil.
2 @PRT,S sql.lab3-1/runacob
3 @ACOB,SE sql.lab3-1/acobskel,TPF$.acobakM1
4 @MAP,S ,TPF$ABS
5 NOT TPF$.
6 IN TPF$.acobskel
7 IN SYS$LIB$*RSA.CBEP$$RSA
8 IN SYS$LIBPRSA.RDMR-ACOBBIAT
9 END
10 @XQT
11 @NWT PRINT$
12 @IPF TPF$.
13 OLD prfil.
14 L END,A,A,NUM
15 MOD SCR
@PRT,S AL3644*SQL.LAB3-1/ACOBSKEL
AL3644*SQL(1).LAB3-1/ACORSKEL(0)
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. acobskel.
3 ENVIRONMENT DIVISION.
4 CONFIGURATION SECTION.
5 SOURCE-COMPUTER. UNISYS-2200.
6 OBJECT-COMPUTER. UNISYS-2200.
7 *
8 **********************************************************
9 *** This program uses interpretive SQL and ACOB to
10 *** solve the lab3-1 assignment for SQL Programming
11 **********************************************************
12 *
13 DATA DIVISION.
14 WORKING-STORAGE SECTION.
15 *
16 **********************************************************
17 *** Establish RDMS command error buffers and data
18 **********************************************************
19 *
20 *** Error handling variables and message buffers
21 *
22 01 room.
23 05 rlin PIC X(40) OCCURS 12 TIMES.
24 01 err-stat PIC 9(4).
25 01 aux-info PIC S1(36).
26 01 error-text.
27 05 err-s PIC X(132) OCCURS 4 TIMES.
28 01 err-s-index PIC 9(4).
29 01 thread-flag PIC 9 VALDE ZERO.
30 88 thread-has-begun VALUE 1.
31 01 cursor-flag PIC 9 VALUE ZERO.
32 88 end-of-cursor VALUE 1.
33 01 err-end-flag PIC 9 VALUE ZERO.
34 88 no-more-mage VALUE 1.
35 *
36 *** Place for the retrieved record, set up for printing
37 *
38 01 record-retrieved.
39 05
40 05
SG 3605 C-1
ACOB Skeleton
41 ************************************************************
42 *** BEGIN the thread, do the USE, clear the SQL buffer
43 ************************************************************
44
45 *** Start the thread
46
47 PROCEDURE DIVISION.
48 000-main.
49 MOVE ' ;' TO room.
50 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
51 IF err-stat NOT = 0 GO TO 900-error-print.
52
53 *** Set table qualifier, clear buffer, set thread flag
54
55 MOVE ' ;' TO room.
56 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
57 GO TO 900-error-print.
58
59 MOVE 1 TO thread-flag.
60
61 ***********************************************************
62 *** Access RDMS - DECLARE and OPEN the cursor
63 ***********************************************************
64
65 *** Declare the cursor
66
67 MOVE ' ' TO room.
68 MOVE ' ' TO rlin(2).
69 MOVE ' ;' TO rlin(3).
70 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
71 IF err-stat NOT = 0 GO TO 900-error-print.
72
73 *** Open the cursor
74
75 MOVE ' ;' TO room.
76 ENTER MASK USING room, err-stat, aux-info.
77 IF err-stat NOT = 0 GO TO 900-error-print.
78
79 **********************************************************
80 ** Loop thru the rows, end the thread, then do cleanup
81 **********************************************************
82
83 *** Loop to fetch a row and display it
84
85 200-loop.
86 MOVE ' ' TO room.
87 MOVE ' ;' TO rlin(2).
88 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info
89
90 IF err-stat = 6001 GO TO 400-drop-cursor.
91 IF err-stat NOT = 0 GO TO 900-error-print.
92
93 DISPLAY record-retrieved UPON PRINTER.
94 GO TO 200-loop.
95
96 400-drop-cursor.
97 DISPLAY 'No more data on fetch' UPON PRINTER.
98 MOVE 'DROP CURSOR employee •' TO room.
99 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
100 IF err-stat NOT = 0 GO TO 900-error-print.
101
102 MOVE SPACES TO room.
103
C-2 SG 3605
ACOB Skeleton
104 ************************************************************
105 *** End the thread, shut down the program
106 ************************************************************
107 *
108 *** Issue the end of the thread
109 *
110 MOVE ' :' TO room.
111 ENTER MASM 'acob$rdmr' USING room, err-stat, aux-info.
112 IF err-stat NOT = 0 GO TO 900-error-print.
113 *
114 *** Stop the program
115 *
116 DISPLAY 'Program has completed' UPON PRINTER.
117 STOP RON.
118 *
119 **********************************************************
120 ** Do the RDMS error handling - display codes, messages
121 **********************************************************
122 *
123 900-error-print.
124 *
125 *** Print errant command and error status variables
126 *
127 DISPLAY 'RDMS Command = ' room UPON PRINTER.
128 DISPLAY 'Error Status = ' err-stat UPON PRINTER.
129 DISPLAY 'Auxiliary•Info = ' aux-info UPON PRINTER.
130 *
131 *** Issue the error messages, end the thread, and exit
132 *
133 MOVE 0 TO err-end-flag.
134 PERFORM 940-geterror UNTIL no-more-msgs.
135 DISPLAY 'Error termination' UPON PRINTER.
136 IF thread-has-begun
137 MOVE ' END THREAD ;' TO room
138 ENTER MASH 'acob$rdmr' USING room, err-stat, aux-info.
139 STOP RUN.
140 *
141 *** Fill the error buffers using GETERROR
142 *
143 940-geterror.
144 MOVE ' ;' TO room.
145 ENTER MASK 'acob$rdmr' USING
146
147 PERFORM 980-print-error
148 VARYING err-s-index FROM 1 BY 1 UNTIL err-s-index > 4.
149 *
150 *** Display the error buffers
151 *
152 980-print-error.
153 IF err-s(err-s-index) NOT = SPACE
154 DISPLAY err-s(err-s-index) UPON PRINTER
155 ELSE
156 MOVE 5 to err-s-index
157 MOVE 1 TO err-end-flag.
SG 3605 C-3
D
ACOB Examples
ACOB Examples
@PRT,S DEV.APPD/RUNACOBPROG1
FURPUR 30R2A (910822 1304:40) 1993 War 17 Wed 1210:56
ANDREWAL3644(1).APPD/RUNACOSPROG1(0)
1 @BREPT PRINT$,PRFIL.
2 @PRT,S DEV.APPD/RUNACOBPROG1
3 @ACON,SE DEV.APPD/ACOSPROG1,TPF$.ACOBPROG1
4 @MAP,S ,TPF$.ABS
5 NOT TPF$.
6 IN TPF$.ACCBPROG1
7 IN SYS$LIBPRSA.CBEP$$RSA
8 IN SYS$LISS*RSA.RDMR-ACOBDAT
9 END
10 @XQT
11 @BRITT PRINT$
12 @IPF TPF$.
13 OLD PRFIL.
14 L END,A,A,NUM
15 MOD SCR
@ACOB, SE DEV . APPD/ACOSPROG1, TPF$ .ACOBPROG1
ACOS 7R1A R 75R3J4Q 03/17/93 12:10:57 (0) 1100 ASCII COBOL 08/12/91 10:02
1100 ASCII COBOL SOURCE LISTING
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. acobprogi.
3 ENVIRONMENT DIVISION.
4 CONFIGURATION SECTION.
5 SOURCE-COMPUTER. UNISYS-2200.
6 OBJECT-COMPUTER. UNISYS-2200.
7 *
8 **********************************************************
9 *** This program uses interpretive SQL and ACOB to
10 *** illustrate simple fetches and displays from a table
11 **********************************************************
12 *
13 DATA DIVISION.
14 WORKING-STORAGE SECTION.
15 *
16 **********************************************************
17 *** Establish RDMS command error buffers and data
18 **********************************************************
19 *
20 *** Error handling variables and message buffers
21 *
22 01 room.
23 05 rlin PIC X(40) OCCURS 12 TIMES.
24 01 err-stat PIC 9(4).
25 01 aux-info PIC S1(36).
26 01 error-text.
27 05 err-s PIC X(132) OCCURS 4 TIMES.
28 01 err-s-index PIC 9(4).
29 01 thread-flag PIC 9 VALUE ZERO.
30 88 thread-has-begun VALUE 1.
31 01 cursor-flag PIC 9 VALUE ZERO.
32 88 end-of-cursor VALUE 1.
33 01 err-end-flag PIC 9 VALUE ZERO.
34 88 no-more-msgs VALUE 1.
SG 3605 D-1
ACOB Examples
35
36 *** Place for the retrieved record, set up for printing
37
38 01 record-retrieved.
39 05 rr-empno PIC 9(5).
40 05 FILLER PIC X(2) VALUE SPACES.
41 05 rr-ename PIC X(12).
42 05 FILLER PIC X(2) VALUE SPACES.
43 05 rr-job PIC X(10).
44 05 FILLER PIC X(2) VALUE SPACES.
45 05 rr-dno PIC 9(3).
46 05 FILLER PIC X(2) VALUE SPACES.
47 05 rr-hiredate PIC 9(6).
48
49 ************************************************************
50 *** BEGIN the thread, do the USE, clear the SQL buffer
51 ************************************************************
52
53 *** Start the thread
54
55 PROCEDURE DIVISION.
56 000-main.
57 MOVE 'BEGIN THREAD FOR udssrc READ TO room.
58 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
59 IF err-stat NOT = 0 GO TO 900-error-print.
60
61 *** Set table qualifier, clear buffer, set thread flag
62
63 MOVE 'USE DEFAULT QUALIFIER rosyschemal TO room.
64 ENTER MASK 'acob$rdmr'USING room, err-stat, aux-info.
65 IF err-stat NOT = 0 GO TO 900-error-print.
66
67 MOVE 1 TO thread-flag.
68
69 ***********************************************************
70 *** Access RDMS - DECLARE and OPEN the cursor
71 ***********************************************************
72
73 *** Declare the cursor
74
75 MOVE 'DECLARE employee CURSOR ' TO room.
76 MOVE ' SELECT empno, ename, job, hiredate, ' TO rlin(2).
77 MOVE ' dno FROM amp ;' To rlin(3).
78 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
79 IF err-stat NOT = 0 GO TO 900-error-print.
80
81 *** Open the cursor
82
83 MOVE 'OPEN employee TO room.
84 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info.
85 IF err-stat NOT = 0 GO TO 900-error-print.
86
87 **********************************************************
88 ** Loop thru the rows, end the thread, then do cleanup
89 **********************************************************
90
91 *** Loop to fetch a row and display it
92
93 200-loop.
94 MOVE 'FETCH employee INTO $P1, $P2, $P3, ' TO room.
95 MOVE ' $P4, $P5 ;' TO rlin(2).
96 ENTER MASK 'acob$rdmr' USING room, err-stat, aux-info,
97 rr-empno, rr-ename, rr-job, rr-hiredate, rr-dno.
98 IF err-stat = 6001 GO TO 400-drop-cursor.
99 IF err-stat NOT = 0 GO TO 900-error-print.
100
D-2 SG 3605
ACOB Examples
SG 3605 D-3
ACOB Examples
@MAP,S ,TPF$.ABS
Collector 33R1A (910808 1521:53) 1993 Mar 17 Wed 1211:02
1. NOT TPF$.
2. IN TPF$.ACOBPROG1
3. IN syssiaNsmsN.cupssNak
4. IN SYS$LIBPRSA.RDMR-ACOBDAT
5. END
AFCM status of output element is SETAFCM
Quarter-word sensitive
ADDRESS LIMITS 001000 002324 725 IBANK WORDS DECIMAL
040000 061765 9206 DRANK WORDS DECIMAL
STARTING ADDRESS 001731
D-4 SG 3605
ACOB Examples
@XQT
05010 FOSTER SALESREP 200 860714
05146 BROWN CLERK 200 871011
05234 WOODWORTH MANAGER 200 790219
05237 ROCKWELL ACCOUNTANT 100 840618
05437 MARTIN SALESREP 200 870326
05469 ADAMS SALESREP 200 811102
05630 GLASS ACCOUNTANT 100 871220
05702 TURNER CLERK 100 890515
05743 LAWSON MANAGER 100 830510
05765 JOHNSON CLERK 400 891221
05784 WILLIAMS PRESIDENT 300 781015
05896 SMITH CLERK 400 880913
05942 FORD SALESREP 200 890316
05984 HYDE MANAGER 400 850901
No more data on Patch
Program has completed
@BRKPT PRINT$
SG 3605 D-5
E
UCOB Skeleton
UCOB Skeleton
@PRT,S AL3644*SQL.LAB3-1/RUNUCOB
FURPUR 30R2A (910822 1304:40) 1993 Mar 17 Wed 1514:48
AL3644*SQL(1).LAB3-1/RUNUCOB(0)
1 @BRKPT PRINT$,prfil.
2 @PRT,S sql.lab3-1/runucob
3 @UCOB,ES sql.lab3-1/ucobskel,tpf$.ucobskel„,no-options,narrow
4 @XQT
5 @BRKPT PRINT$
6 @IPF tpf$.
7 OLD prfil.
8 1 "END",e,e,numb
9 mod scr
@PRT,S AL3644*SQL.LAB3-1/UCOBSKEL
AL3644*SQL(1).LAB3-1/UCOBSKEL(0)
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. ucobskel.
3 ENVIRONMENT DIVISION.
4 CONFIGURATION SECTION.
5 SPECIAL-NAMES. PRINTER IS printer.
6
7 **********************************************************
8 *** This program uses embedded SQL and UCOB to
9 *** solve lab 3-1 assignment for SQL Programming
10 **********************************************************
11
12 DATA DIVISION.
13 WORKING-STORAGE SECTION.
14
15 01 thread-flag PIC 9 VALUE ZERO.
16 01 err-end-flag PIC 9 VALUE ZERO.
17 88 no-more-msgs VALUE 1.
18
19 **********************************************************
20 *** Establish RDMS command error buffers and data
21 **********************************************************
22
23 EXEC SQL BEGIN DECLARE SECTION END-EXEC.
24
25 *** Place for the retrieved record, set up for printing
26
27 01 record-retrieved.
28 05
29 05
30
31 *** Error handling variables and message buffers
32
33 01 sqlcode PIC S9(9) USAGE COMP.
34
35 01 rdmca.
36 05 error-status PIC 9(4).
37 05 aux-info PIC S9(9) USAGE BINARY.
38
39 01 err-text.
40 05 err-s PIC X(132) OCCURS 4 TIMES.
41
42 01 err-s-index PIC 9.
43 EXEC SQL END DECLARE SECTION END-EXEC.
44
SG 3605 E-1
UCOB Skeleton
45 **********************************************************
46 *** Start by setting up error handling and BEGIN thread
47 **********************************************************
48
49 PROCEDURE DIVISION.
50 000-main.
51
52 *** What to do on no-find
53
54 EXEC SQL
55
56 END-EXEC.
57
58 *** What to do if an SQL error is encountered
59
60 EXEC SQL
61
62 END-EXEC.
63
64 *** Start the thread
65
66
67 MOVE 1 TO thread-flag.
68
69 **********************************************************
70 ** Access RDMS - USE, DECLARE, OPEN and initial FETCH
71 **********************************************************
72
73 *** Set up the table qualifier
74
75 EXEC SQL
76
77 END-EXEC.
78
79 *** Declare the cursor
80
81 EXEC SQL
82
83
84
85 END-EXEC.
86
87 *** Open the cursor
88
89 EXEC SQL
90
91 END-EXEC.
92
93 *** Do an initial fetch of a row
94
95 EXEC SQL
96
97
98 END-EXEC.
99
E-2 SG 3605
UCOB Skeleton
100 **********************************************************
101 ** Loop thru the rows, end the thread, then do cleanup
102 **********************************************************
103
104 *** Loop to display a row and get another
105
106 PERFORM UNTIL sqlcode = 100
107 DISPLAY record-retrieved
108 EXEC SQL
109 FETCH
110
111 END-EXEC
112 END-PERFORM.
113
114 **********************************************************
115 *** Handle a no-find, end thread, and terminate program
116 **********************************************************
117
118 *** Come here when a no find condition exists
119
120 400-no-find.
121
122 *** Issue the end of the thread
123
124
125 MOVE 0 TO thread-flag.
126
127 *** Stop the program
128
129 DISPLAY 'Program has completed' UPON PRINTER.
130 STOP RUN.
131
SG 3605 E-3
UCOB Skeleton
132 **********************************************************
133 ** Do the RDMS error handling - display codes & messages
134 **********************************************************
135
136 *** Come here when RDMS errors are detected
137
138 900-rdms-error.
139
140 *** Turn off error handling, print error status variables
141
142 EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
143
144 DISPLAY '***SQL Code = ' sqlcode UPON PRINTER.
145 DISPLAY '***Error Code = ' error-status UPON PRINTER.
146 DISPLAY '***Auxiliary Info = ' aux-info UPON PRINTER.
147
148 *** Issue the error messages, end the thread, and exit
149
150 MOVE 0 TO err-end-flag.
151 PERFORM 940-geterror UNTIL no-more-msgs.
152 DISPLAY 'Error termination' UPON PRINTER.
153 END THREAD.
154 STOP RUN.
155
156 *** Fill the error buffers using GETERROR
157
158 940-geterror.
159
160 PERFORM 980-print-error
161 VARYING err-s-index FROM 1 BY 1 UNTIL err-s-index > 4.
162
163 *** Display the error buffers
164
165 980-print-error.
166 IF err-s(err-s-index) NOT = SPACE
167 DISPLAY err-s(err-s-index) UPON PRINTER
168 ELSE
169 MOVE 5 to err-s-index
170 MOVE 1 TO err-end-flag.
@BRXPT PRINT$
E-4 SG 3605
F
UCOB Examples
UCOB Examples
@PRT,S DEV.APPF/RUNUCOBPROG1
FURPUR 30R2A (910822 1304:40) 1993 Mar 17 Wed 1312:51
ANDREW*AL3644(1).APPF/RUNUCOBPROG1(0)
1 @BRKPT PRINT$,prfil.
2 @PRT,S dev.appf/runucobprogl
3 @UCOB,ES dev.appf/ucobprogl,tpf$.ucobprogl,,,no-options,narrow
4 @XQT
5 @BRKPT PR/NT$
6 @IPF tpf$.
7 OLD prfil.
8 1 "END",a,a,numb
9 mod scr
@UCOB,ES DEV.APPF/UCOBPROG1,TPF$.UCOBPROG1„,NO-OPTIONS,NARROW
*REMARK(CLARIFICATION) LSS-CSIM20389: Option REMARK has been previously
set
UCOB- 5R2(910816) LSS- 6R1A(910814) 930317 13:12:52
1 IDENTIFICATION DIVISION.
2 PROGRAM-ID. ucobprogl.
3 ENVIRONMENT DIVISION.
4 CONFIGURATION SECTION.
5 SPECIAL-NAMES. PRINTER IS printer.
6
7 **********************************************************
8 *** This program uses embedded SQL and UCOB to
9 *** solve lab exercise 3-1 for AL 3644.
10 **********************************************************
11
12 DATA DIVISION.
13 WORKING-STORAGE SECTION.
14
15 01 thread-flag PIC 9 VALUE ZERO.
16 01 err-end-flag PIC 9 VALUE ZERO.
17 88 no-more-msgs VALUE 1.
18
19 **********************************************************
20 *** Establish RDMS command error buffers and data
21 **********************************************************
22
23 EXEC SQL BEGIN DECLARE SECTION END-EXEC.
24
25 *** Place for the retrieved record, set up for printing
26
27 01 record-retrieved.
28 05 rr-empno PIC 9(5) VALUE ZEROS.
29 05 PIC X(2) VALUE SPACES.
30 05 rr-ename PIC X(12) VALUE SPACES.
31 05 PIC X(2) VALUE SPACES.
32 05 rr-job PIC X(10) VALUE SPACES.
33 05 PIC X(2) VALUE SPACES.
34 05 rr-dno PIC 9(3) VALUE ZEROS.
35 05 PIC X(2) VALUE SPACES.
36 05 rr-hiredate PIC 9(6) VALUE ZEROS.
37 05 PIC X(2) VALUE SPACES.
38
SG 3605 F-1
UCOB Examples
F-2 SG 3605
UCOB Examples
100
101 *** Do an initial fetch of a row
102
103 EXEC SQL
104 FETCH emp INTO :rr-empno, :rr-ename,
105 :rr-job, :rr-hiredate, :rr-dno
106 END-EXEC.
107
108 **********************************************************
109 ** Loop thru the rows, end the thread, then do cleanup
110 **********************************************************
111
112 *** Loop to display a row and get another
113
114 PERFORM UNTIL sqlcode = 100
115 DISPLAY record-retrieved
116 EXEC SQL
117 FETCH emp INTO :rr-empno, :rr-ename,
118 :rr-job, :rr-hiredate, :rr-dno
119 END-EXEC
120 END-PERFORM.
121
122 **********************************************************
123 *** Handle a no-find, end thread, and terminate program
124 **********************************************************
125
126 *** Come here when a no find condition exists
127
128 400-no-find.
129
130 *** Issue the end of the thread
131
132 END THREAD.
133 MOVE 0 TO thread-flag.
134
135 *** Stop the program
136
137 DISPLAY 'Program has completed' UPON PRINTER.
138 STOP RUN.
139
140 **********************************************************
141 ** Do the RDMS error handling - display codes & messages
142 **********************************************************
143
144 *** C mo here when RDMS errors are detected
145
146 900-rdms-error.
147
148 *** Turn off error handling, print error status variables
149
150 EXEC SQL WHENEVER SQLERROR CONTINUE END-EXEC.
151
152 DISPLAY '***SQL Code = ' sqlcode UPON PRINTER.
153 DISPLAY '***Error Code = ' error-status UPON PRINTER.
154 DISPLAY '***Auxiliary Info = ' aux-info UPON PRINTER.
SG 3605 F-3
UCOB Examples
155 *
156 *** Issue the error messages, end the thread, and exit
157 *
158 MOVE 0 TO err-end-flag.
159 PERFORM 940-geterror UNTIL no-more-msgs.
160 DISPLAY 'Error termination' UPON PRINTER.
1.61 END THREAD.
162 STOP RUN.
163 *
164 *** Fill the error buffers using GETERROR
165 *
166 940-geterror.
167 GETERROR INTO :err-s(1), :err-s(2), :err-s(3), :err-s(4).
168 PERFORM 980-print-error
169 VARYING err-s-index FROM 1 BY 1 UNTIL err-s-index > 4.
170 *
171 *** Display the error buffers
172
173 980-print-error.
174 IF err-s(err-s-index) NOT = SPACE
175 DISPLAY err-s(err-s-index) UPON PRINTER
176 ELSE
177 MOVE 5 to err-s-index
178 MOVE 1 TO err-end-flag.
SIZES: CODE(EM4): 448 DATA: 1193
END UCOB- 0 ERRORS(MAJOR) 0 ERRORS(MINOR) 1 REMARK(CLARIFICATION)
@XQT
5010 FOSTER SALESREP 200 860714
5146 BROWN CLERK 200 871011
5234 WOODWORTH MANAGER 200 790219
5237 ROCKWELL ACCOUNTANT 100 840618
5437 MARTIN SALESREP 200 870326
5469 ADAMS SALESREP 200 811102
5630 GLASS ACCOUNTANT 100 871220
5702 TURNER CLERK 100 890515
5743 LAWSON MANAGER 100 830510
5765 JOHNSON CLERK 400 891221
5784 WILLIAMS PRESIDENT 300 781015
5896 SMITH CLERK 400 880913
5942 FORD SALESREP 200 890316
5984 HYDE MANAGER 400 850901
Program has completed
@BRKPT PRINT$
F-4 SG 3605