You are on page 1of 12

DB2

System tables
GROUP BY, HAVING
LTRIM RTRIM
-904, HOW TO determine cause in spufi, DIS DATABASE (bc*) STATUS of RW,
CHKP, COPY
LOAD NOCOPYPEND (to load without causing copypend status)
EXPLAIN, sql performance
REPAIR, START, STOP,
DSNUTIL
SCALAR function examples DATE, SUBSTR, TIME, VALUE,
COLUMN functions examples MIN, MAX, AVG, COUNT, DISTINCT
COALESCE VALUE
NULL IND, sqlcode -305
-811, -805, -803
-911 deadlock causes succesfull, -913 deadlock causes unsuccesful rollback
VIEW, to restrict specific column access
VSAM & flat file, access to VSAM, dynamic and random and sequential
CS and RR
Difference between left outer join and Inner Join
Inner Join all conditions in Where must be met by row in both tables
Left Outer Join syntactically: does not use WHERE, uses ON
- even if row in Left table does not meet ON condition, left table row is
in the results table
If I were TO write all contents of a db2 table to a flat file, how would I write this
processing in my in program
If I had a nullable field, how do I code to prevent error in accessing null? Answer: by
using Null-ind, if -1, fields has nulls.
How to code without using Null-ind? Answer: Coalesce, or Value
What to code in cursor if there are columns to be updated?
If I had already written my COBOL DB2 program, what steps do I have to take to make
it executable: Precompile, (writes to DBRM), Compile, link-edit, Bind (Using
optimizer, finds which path to take)
What is a collection in DB2? Answer : group of associated packages
What is a package in DB2?
How do you dynamically change package in program? Answer
EXEC SQL SET PACKAGESET (XXXX)
What are the common SQLCODES encountered with DB2 Applications?

+100 - No row met the search conditions specified in an UPDATE or DELETE


statement; The result of a SELECT INTO statement was an empty table; A FETCH
statement was executed when the cursor was positioned after the last row of the result
table; The result of the subselect of an INSERT statement is empty.
+0 - found condition / no error condition
-803 - Execution of the requested INSERT or UPDATE would result in duplicate values
occurring on the unique index. The update might also be caused by a DELETE operation
of a parent row that cascades to a dependent row with a delete rule of SET NULL.
-811 - Execution of an embedded SELECT statement or a subselect in the SET clause
of an UPDATE statement has resulted in a result table that contains more than one row.
Alternatively, a subquery contained in a basic predicate has produced more than one
value.
-818 - The precompiler-generated timestamp in the load module is different from the
bind timestamp in the DBRM.
-805 - An application program attempted to use a DBRM or package that was not found
on the plan.
-911 - The current unit of work was the victim in a deadlock, or experienced a timeout,
and had to be rolled back

How would you correct an SQLCODE of 803?

Make sure that a unique index is used


Change the error handling logic inside the application (e.g. If SQLCODE= -803 Then
Exec SQL Update)
In the application, do a SELECT prior to INSERT or UPDATE. If row is found, do an
UPDATE. Otherwise, do an INSERT.

What is CLUSTERED INDEX? Why do we need an index to be CLUSTERED?

Consider creating additional indexes on other columns based on how often you expect the
column to be used in search criteria. Once you have identified all the desired indexes,
decide which column is apt to be used most often in search criteria. Then load the table in
that column's sequence, thus making the column's corresponding index a CLUSTERED
index.
For efficiency

How would you handle an SQLCODE of 811?

Although not advisable, the easiest way of eliminating a 811 is by using the DISTINCT
clause on the SELECT statement (e.g. Exec SQL Select DISTINCT)
Check if the WHERE clause used on the SELECT comprises the unique index of the
table. If not unique, either ALTER the index of the table or change the WHERE clause on
the application such that it uses all columns of the unique index.
If multiple rows is really expected, Utilize a CURSOR

What are the different logical steps in employing a cursor?

DECLARE CURSOR
OPEN
Perform a FETCH routine until SQLCODE is +100
CLOSE CURSOR

What would you do if you encountered an SQLCODE +100 on OPEN cursor?

There is no SQLCODE +100 on OPEN cursor

What are the different types of cursor? (Introduced on DB2 Version 7)

NON-SCROLLABLE - The simplest type of cursor is a non-scrollable cursor. A nonscrollable cursor always moves sequentially forward in the result table. You cannot
retrieve rows backward or move to a specific position in a result table with a nonscrollable cursor.
SCROLLABLE opposite of the definition above. This type of cursor is created by
using the SCROLL clause on the DECLARE

What is SQLCODE 502? Would the application abend if not handled?

Error is raised by DB2 when an OPEN cursor is executed to an already open cursor
No data changes, the application does not abend

What is an SQLCODE 501? Would the application ABEND if not handled?

SQLCODE is raised when a CLOSE cursor is executed on a cursor that is not open
No data changes, the application does not ABEND.

What causes an SQLCODE 818? How would you handle/correct an SQLCODE 818?

The SQL precompiler places timestamp in the DBRM and in the load module of the
application program. At run-time, the two timestamps are compared with each other. If
the two timestamps do not match, the DBRM and the load module were not the result of
the same precompile. An 818 condition is then raised.
Recompile and bind the program

What are the different causes of an SQLCODE 805? How would you handle/correct
this error?

The DBRM name was not found in the member list of the plan and there is no package
list for the plan.
The package name was not found because there is no package list for the plan.
The 'location-name' in the package list was not correct when the application was bound.
The application was not connected to the proper location.
Rebind would correct the error
If due to wrong location name or connected to the wrong location, make sure that the
application connects to the right location at runtime.

What is deadlock (give a scenario)? What can you do with an SQLCODE 911?

PROGRAM A has ROW Y locked and PROGRAM B has ROW X locked. Later on,
PROGRAM A requests for ROW X and was suspended since PROGRAM B currently
locks the row. Then PROGRAM B request for ROW Y and was suspended since
PROGRAM A currently locks the row. At this point, PROGRAM A and PROGRAM B
are in DEADLOCK. DB2 would then try to rollback changes made by both programs. If
the rollback is successful, an SQLCODE 911 is raised. Otherwise, an SQLCODE 913
is raised.
Rerun the program and hope that the scenario above is not repeated.

Use the WITH UR clause on the SELECT statements on both programs for minimal row
locking.

What does an EXPLAIN do? When would you use the statement?

EXPLAIN is used as a BIND option [EXPLAIN (YES)]


Can be invoked by executing the SQL EXPLAIN statement either statically or
dynamically (QMF or SPUFI).
EXPLAIN is a monitoring tool that obtains information about the access paths for all
explainable SQL statements in a package or the DBRMs of a plan (the information
appears in table PLAN_TABLE); obtains information on estimated cost of executing an
SQL SELECT, INSERT, UPDATE or DELETE statement (the information appears in
table DSN_STATEMENT_TABLE); obtains information on User-defined functions
referred to in a SQL statement, which includes the specific name and schema (the
information appears in table DSN_FUNCTION_TABLE).
Information gathered by EXPLAIN can be of help for designing databases, indexes, and
application programs. This information can also tell when to rebind an application or can
be of help in determining the access path chosen for a query.

What are the different data types in DB2?

SMALLINT PIC S9(4) usage comp


INTEGER PIC S9(9) usage comp
DECIMAL (p,s) PIC S9(p-s)V9(s) usage comp-3
CHAR(n) PIC X(n)
VARCHAR(n)
10 FIELD-NAME.
49 FIELD-NAME-LEN
PIC 9(4) usage comp
49 FIELD-NAME-TEXT PIC X(n).
DATE PIC X(10)
TIMESTAMP PIC X(26)
CLOB USAGE IS SQL TYPE IS CLOB-LOCATOR
BLOB USAGE IS SQL TYPE IS BLOB-LOCATOR
DBCLOB USAGE IS SQL TYPE IS DBCLOB-LOCATOR
ROWID USAGE IS SQL TYPE IS ROWID

What is VARCHAR?

Is a variable length string field

What are the steps in preparing to run a COBOL/DB2 program?

Precompile
Bind
Compile
Link Edit

What does BIND do?

Check authorizations
Check DB2 access paths

Creates the most effective path

When do you need to rebind?

Table change
DB2 upgrade
Runstats was run causing new row count

What is a plan? When will it be created?

A PLAN is a control structure that is used to execute SQL statements. The control
structures can be thought of as the bound or operational form of SQL statements taken
from a database request module (DBRM). It can also contain a list of package names. It
relates an application process to a local instance of DB2, specifying the processing
options.
The control structure that is contained inside a plan is generated when a BIND PLAN or a
BIND PACKAGE is executed. Thus, a PLAN is created after the successful execution of
a DB2 BIND subcommand.

What is a view? When is it needed? When will you use it?

A view provides an alternative way of looking at the data in one or more tables. A view is
a named specification of a result table. The specification is an SQL SELECT statement
that is effectively executed whenever the view is referenced in an SQL statement. At any
time, the view consists of the rows that would result if the subselect were executed. Thus,
a view can be thought of as having columns and rows just like a base table. However,
columns added to the base tables after the view is defined do not appear in the view. For
retrieval, all views can be used like base tables.
Views can be used to control access to a table and make data easier to use. Access to a
view can be granted without granting access to the table. The view can be defined to
show only portions of data in the table. A view can show summary data for a given table,
combine two or more tables in meaningful ways, or show only the selected rows that are
pertinent to the process using the view.

What is referential integrity?

The state of a database in which all values of all foreign keys are valid. Maintaining
referential integrity requires the enforcement of referential constraints on all operations
that change the data in a table upon which the referential constraints are defined.

Explain Primary Key and Foreign Key concepts

PRIMARY KEY is a unique, non-null key that is part of the definition of a table. A table
cannot be defined as a parent unless it has a unique key or primary key.
FOREIGN KEY is a column or set of columns in a dependent table of a constraint
relationship. The key must have the same number of columns, with the same descriptions,
as the primary key of the parent table. Each foreign key value must either match a parent
key value in the related parent table or be null.

What are subqueries?


Inner Join
Left outer join

Full outer join


Right outer join
Coalesce

What are null indicators?

A NULL INDICATOR is an indicator variable that could be used to determine:


whether the value of an associated output host variable is null or indicate that an
input host variable value is null
the original length of a character string that was truncated during assignment to a
host variable
that a character value could not be converted during assignment to a host variable
the seconds portion of a time value that was truncated during assignment to a
host variable

What is DCLGEN?

DCLGEN is a subcomponent of DB2 that generates SQL table declarations and COBOL,
C, or PL/I data structure declarations that conform to the table. The declarations are
generated from DB2 system catalog information. DCLGEN is also a DSN subcommand.

What are SCALAR and COLUMN functions?

Column functions is an operation that derives its result by using values from one or more
rows and produces a single value
Scalar functions return values to each row

Explain different utilities used


LOAD is used to load one or more tables of a table space. It loads records into the

tables and builds or extends any indexes defined on them. If the table space already
contains data, you can choose whether you want to add the new data to the existing data
(LOAD RESUME) or replace the existing data (LOAD REPLACE). The loaded data is
processed by any edit or validation routine associated with the table, and any field
procedure associated with any column of the table
RUNSTATS - The RUNSTATS online utility gathers summary information about the
characteristics of data in table spaces, indexes, and partitions. DB2 records this
information in the DB2 catalog and uses it to select access paths to data during the bind
process. It is available to the database administrator for evaluating database design and to
aid in determining when table spaces or indexes must be reorganized. There are two
formats for the RUNSTATS utility: RUNSTATS TABLESPACE and RUNSTATS
INDEX. RUNSTATS TABLESPACE gathers statistics on a table space and, optionally,
on indexes or columns; RUNSTATS INDEX gathers statistics only on indexes. To
update the catalog statistics, run the RUNSTATS utility after a LOAD or REBUILD
INDEX job.
REPAIR - The REPAIR online utility repairs data. The data can be your own data, or
data you would not normally access, such as space map pages and index entries. It can
be used to:
Reset a pending status on a table space or index
Verify the contents of data areas in table spaces and indexes
Replace the contents of data areas in table spaces and indexes

Delete a single row from a table space


Produce a hexadecimal dump of an area in a table space or index
Delete an entire LOB from a LOB table space
Dump LOB pages
QUIESCE - The QUIESCE online utility establishes a quiesce point (the current log
RBA or log record sequence number (LRSN)) for a table space, partition, table space set,
or list of table spaces and table space sets, and records it in the SYSIBM.SYSCOPY
catalog table. A successful QUIESCE improves the probability of a successful
RECOVER or COPY. You should run QUIESCE frequently between regular executions
of COPY to establish regular recovery points for future point in time recovery

How do you add columns in DB2?

Add a column to an existing DB2 table using the ADD COLUMN-NAME of the ALTER
TABLE subcommand

What is COMMIT and ROLLBACK?

Both operations ends a unit of work


COMMIT is an operation that ends a unit of work by releasing locks so that the database
changes that are made by that unit of work can be perceived by other processes
ROLLBACK is the process of restoring data changed by SQL statements to the state at
its last commit point. All locks are freed

Explain your experience on DB2 tables used in your company


Answers to follow
Discuss Normalization and Denormalization
Normalized faster update / slower retrieval

Denormalized faster retrieval / slower update

JCL:
PARM
DISP
Symbolic Parameter
JCL Questions:
(Interview last Feb 14, 2006)
1. What generation number will y
2. ou use to create a new dataset?
3. What generation number is used to refer to a dataset created from a previous step?

4. If an abend has occurred and you do a rerun, what generation number will you use
at the restart step?
5. How do you pass a parameter from a JCL to a cobol program?
6. What will you check in your cobol program to determine if you were able to pass
a value from your JCL? (thru linkage section)
7. What is the total number of tracks used in SPACE = TRK,(5,5) ?

Cobol Questions:
(Interview last Feb 14, 2006)
1.
2.
3.
4.

Difference between a binary search and linear search.


What is reference modification? (i.e. WS-DATE-YEAR (1:2)
Difference between regular Perform and Inline Perform
In Perform A Until xxx , when do you test condition? Answer: test UNTIL first
before entering paragraph.
5. What to code if you want to enter paragraph before doing test of UNTIL?
Answer: Code Perform Para-A until cond-1 WITH TEST BEFORE
6. How does cobol implelent DO WHILE
7. How does cobol implement DO UNTIL
8. How does Evaluate statement work? What do you call it - Case Structure
9. If I had PIC X(50), how to find out how many As there are in field? Answer
Inspect Tallying
10. What is the PIC clause definition of a zoned decimal? of binary? of packed
decimal?
11. How do you compute for the length of a packed decimal?
12. How do you initialize an internal table?
13. Can you initialize on the OCCURS level?
14. What is a use of a REDEFINES?
15. How do you perform an internal sort?
16. What do you do after INPUT PROCEDURE in an internal sort?
17. Give examples of exception errors and its causes. (SOC4, SOC7)
18. How would you know if your index is out of bounds?
10. In the ff. statements, how many times will it perform the paragraph?
a. MOVE 5 TO A
PERFORM Paragraph-Name UNTIL A = 5
b. MOVE 5 TO A
PERFORM VARYING A FROM 1 BY 1 UNTIL A > 5
19. File matching logic
20. Difference between static and dynamic call
21. Difference between index vs. subscript
22. If I had a file, and I forgot to put DD statement in JCL, what happens? Answer:
will not abend until OPEN or any I-O statement is operated on file, then if Open

Input, abend will occur, If Open Output, no abend will occur, program normally
terminates but no file created
How do you perform internal sort?

2 ways:
1.) USING, GIVING sorts are comparable to external sorts with no extra file
processing;
2.) INPUT PROCEDURE, OUTPUT PROCEDURE sorts allow for data
manipulation before and/or after the sort
Syntax:
SORT file-1 ON ASCENDING/DESCENDING KEY key....
USING file-2
GIVING file-3.
USING can be substituted by INPUT PROCEDURE IS para-1 THRU para-2
GIVING can be substituted by OUTPUT PROCEDURE IS para-1 THRU para-2.
file-1 is the sort workfile and must be described using SD entry in FILE
SECTION
file-2 is the input file for the SORT and must be described using an FD entry in
FILE SECTION and SELECT clause in FILE CONTROL.
file-3 is the outfile from the SORT and must be described using an FD entry in
FILE SECTION and SELECT clause in FILE CONTROL.
file-1, file-2 & file-3 should not be opened explicitly.
INPUT PROCEDURE is executed before the sort and records must be
RELEASEd to the sort work file from the input procedure.
OUTPUT PROCEDURE is executed after all records have been sorted. Records
from the sort work file must be RETURNed one at a time to the output procedure.

How do you initialize a 2 dimensional array?

Use perform varying phrase to initialize table


If table is indexed, do not use INITIALIZE but use MOVE instead to move
spaces or zeroes.

What's the length of PIC S9(01) COMP? S9(05) COMP?

PIC S9(01) COMP? 2 bytes


PIC S9(05) COMP? 4 bytes

How do you compute for the length of comp-3?

General formula is INT((n/2) + 1))


Say S9(7) comp 3, how many bytes does it occupy? 4 bytes

Given a pic 9(2) field with a value of 10 and moved to a pic x(3) field. What will be
the value of the receiving field?

Answer: 10_
Numeric integer fields can be moved to alphanumeric fields but numeric fields
with a V in the pic clause cannot be moved to alphanumeric fields.

What are the causes for S0C1, S0C4, S0C5, S0C7, S0CB abends?

S0C1 - May be due to 1.Missing or misspelled DD name 2.Read/Write to


unopened dataset 3.Read to dataset opened output 4.Write to dataset opened input
5.Called subprogram not found
S0C4 may be due to 1.Missing Select statement(during compile) 2.Bad
Subscript/index 3.Protection Exception 4.Missing parameters on called
subprogram 5.Read/Write to unopened file 6.Move data from/to unopened file.
S0C5 May be due to 1.Bad Subscript/index 2.Close an unopened dataset 3.Bad
exit from a perform 4.Access to I/O area (FD) before read.
S0C7 may be due to 1.Numeric operation on non-numeric data 2.Un-initialize
working-storage 3.Coding past the maximum allowed sub script.
S0CB may be due to 1.Division by Zero

Binary search vs linear search?

In a binary search the table element key values must be in ascending or


descending sequence. The table is 'halved' to search for equal to, greater than or
less than conditions until the element is found. In a sequential search the table is
searched from top to bottom, so (ironically) the elements do not have to be in a
specific sequence.
The binary search is much faster for larger tables, While sequential Search works
well with smaller ones.
SEARCH ALL is used for binary searches; SEARCH for sequential.

- file matching logic (1: many file matching and many:many file matching)
What is the difference between a dynamic call and static call?

Static call statement results in the called subprogram being link-edited with the
main program into one load module. The dynamic call results in the dynamic
invocation of a separate load module.
A statically called subroutine will not be in its initial state the next time it is called
unless you explicitly use INITIAL or you do a CANCEL. A dynamically called
routine will always be in its initial state.

What is file status 92?

Logic error. e.g., a file is opened for input and an attempt is made to write to it.

What is file status 39 ?

Mismatch in LRECL or BLOCKSIZE or RECFM between your COBOL pgm &


the JCL (or the dataset label).

How will you position an indexed file at a specific point so that the subsequent
sequential operations on the file can start from this point?

Use Start
START filename KEY IS EQ/GT/LT.. dataname
INVALID KEY ...

Access mode must be SEQUENTIAL or DYNAMIC

What is the difference between a subscript and an index in a table definition?

A subscript is a working storage data definition item, typically a PIC (999)


where a value must be moved to the subscript and then increment or
decrement it by ADD TO and SUBTRACT FROM statements. An index
is a register item that exists outside the program's working storage. You
SET an index to a value and SET it UP BY value and DOWN BY value.

Subscript refers to the array occurrence while index is the displacement (in
no of bytes) from the beginning of the array.

An index can only be modified using PERFORM, SEARCH & SET. Need
to have index for a table in order to use SEARCH, SEARCH ALL Cobol
statements

What is the difference between GO BACK and STOP RUN? What happens
if you use GO BACK instead of STOP RUN in a stand-alone program?

Program will have a normal termination.

What is the difference between NEXT SENTENCE and CONTINUE?

NEXT SENTENCE gives control to verb following the next period.


CONTINUE gives control to the next verb after the explicit scope
terminator.

Can I redefine an x(2) field with a field of x(4)?

Yes. Redefines only causes both fields to start the same location.

CICS
If I had a new transaction with maps, programs and files to access, what do I need to
define in CICS:
PPT Processing Program Table for Cobol program and Map
PCT Program Control Table for Trans-id and first program to execute
FCT For VSAM file
Where to define map, in PPT
What happens internally in CICS system once you enter trans-id on screen
Difference between Pseudo-conversational and conversational

You might also like