DB2 (part-3) Interview question and answers

What is DCLGEN ?

DCLGEN stands for declarations generator; it is a facility to generate DB2 sql data structures in COBOL or PL/I programs.

How do you leave the cursor open after issuing a COMMIT? (for DB2 2.3 or above only)

Use WITH HOLD option in DECLARE CURSOR statement. But, it has not effect in psuedo-conversational CICS programs.

Give the COBOL definition of a VARCHAR field.

A VARCHAR column REMARKS would be defined as follows:
... 10 REMARKS.
49 REMARKS-LEN PIC S9(4) USAGE COMP.
49 REMARKS-TEXT PIC X(1920).

What is the physical storage length of each of the following DB2 data types: DATE, TIME, TIMESTAMP?

DATE: 4bytes
TIME: 3bytes
TIMESTAMP: 10bytes

What is the COBOL picture clause of the following DB2 data types: DATE, TIME, TIMESTAMP?

DATE: PIC X(10)
TIME : PIC X(08)
TIMESTAMP: PIC X(26)

What is the COBOL picture clause for a DB2 column defined as DECIMAL(11,2)? - GS

PIC S9(9)V99 COMP-3.
Note: In DECIMAL(11,2), 11 indicates the size of the data type and 2 indicates the precision.

What is DCLGEN ? - GS

DeCLarations GENerator: used to create the host language copy books for the table definitions. Also creates the DECLARE table.

What are the contents of a DCLGEN? - GS

EXEC SQL DECLARE TABLE statement which gives the layout of the table/view in terms of DB2 datatypes.
A host language copy book that gives the host variable definitions for the column names.

Is it mandatory to use DCLGEN? If not, why would you use it at all? - GS

It is not mandatory to use DCLGEN. Using DCLGEN, helps detect wrongly spelt column names etc. during the pre-compile stage itself (because of the DECLARE TABLE ). DCLGEN being a tool, would generate accurate host variable definitions for the table reducing chances of error.

Is DECLARE TABLE in DCLGEN necessary? Why it used?

It not necessary to have DECLARE TABLE statement in DCLGEN. This is used by the pre-compiler to validate the table-name, view-name, column name etc., during pre-compile.

Will precompile of an DB2-COBOL program bomb, if DB2 is down?

No. Because the precompiler does not refer to the DB2 catalogue tables.

How is a typical DB2 batch program executed ?

There are two methods of executing a DB2-batch program
1. Use DSN utility to run a DB2 batch program from native TSO. An example is shown:
DSN SYSTEM(DSP3)
RUN PROGRAM(EDD470BD) PLAN(EDD470BD) LIB('EDGS01T.OBJ.LOADLIB')
END
2. Use IKJEFT01 utility program to run the above DSN command in a JCL. Q13) Assuming that a site's standard is that pgm name = plan name, what is the easiest way to find out which programs are affected by change in a table's structure?
Query the catalogue tables SYSPLANDEP and SYSPACKDEP.

Name some fields from SQLCA.

SQLCODE, SQLERRM, SQLERRD

How can you quickly find out the number of rows updated after an update statement?

Check the value stored in SQLERRD(3).

What is EXPLAIN? - GS

EXPLAIN is used to display the access path as determined by the optimizer for a SQL statement. It can be used in SPUFI (for single SQL statement) or in BIND step (for embedded SQL). The results of EXPLAIN is stored in U.PLAN_TABLE where U is the authorization id of the user

What do you need to do before you do EXPLAIN?

Make sure that the PLAN_TABLE is created under the AUTHID.

Where is the output of EXPLAIN stored? - GS

In USERID.PLAN_TABLE

EXPLAIN has output with MATCHCOLS = 0. What does it mean? - GS

A nonmatching index scan if ACCESSTYPE = I.

How do you do the EXPLAIN of a dynamic SQL statement?

There are two methods to achieve this:
1. Use SPUFI or QMF to EXPLAIN the dynamic SQL statement
2. Include EXPLAIN command in the embedded dynamic SQL statements

How do you simulate the EXPLAIN of an embedded SQL statement in SPUFI/QMF? Give an example with a host variable in WHERE clause)

Use a question mark in place of a host variable (or an unknown value). For instance,
SELECT EMP_NAME FROM EMP WHERE EMP_SALARY > ?

What are the isolation levels possible ? - GS

CS: Cursor Stability
RR: Repeatable Read

What is the difference between CS and RR isolation levels?

CS: Releases the lock on a page after use
RR: Retains all locks acquired till end of transaction

When do you specify the isolation level? How?

During the BIND process(ISOLATION LEVEL is a parameter for the bind process). ISOLATION ( CS/RR )...

I use CS and update a page. Will the lock be released after I am done with that page?

No.

What are the various locking levels available?

PAGE, TABLE, TABLESPACE

How does DB2 determine what lock-size to use?

There are three methods to determine the lock-size. They are:
1. Based on the lock-size given while creating the tablespace
2. Programmer can direct the DB2 what lock-size to use
3. If lock-size ANY is specified, DB2 usually choses a lock-size of PAGE

What are the disadvantages of PAGE level lock?

High resource utilization if large updates are to be done

What is lock escalation?

Promoting a PAGE lock-size to table or tablespace lock-size when a transaction has aquired more locks than specified in NUMLKTS. Locks should be taken on objects in single tablespace for escalation to occur.

What are the various locks available?

SHARE, EXCLUSIVE, UPDATE

Can I use LOCK TABLE on a view?

No. To lock a view, take lock on the underlying tables.

What is ALTER ? - GS

SQL command used to change the definition of DB2 objects.

What is a DBRM, PLAN ?

DBRM: Data Base Request Module, has the SQL statements extracted from the host language program by the pre-compiler. PLAN: A result of the BIND process. It has the executable code for the SQL statements in the DBRM.
Determine the point at which DB2 acquires or releases locks against table and tablespaces, including intent locks.

What else is there in the PLAN apart from the access path? - GS

PLAN has the executable code for the SQL statements in the host program

What happens to the PLAN if index used by it is dropped?

Plan is marked as invalid. The next time the plan is accessed, it is rebound.

What are PACKAGES ? - GS

They contain executable code for SQL statements for one DBRM.

What are the advantages of using a PACKAGE?

The advantages of using PACKAGE are:
1. Avoid having to bind a large number of DBRM members into a plan
2. Avoid cost of a large bind
3. Avoid the entire transaction being unavailable during bind and automatic rebind of a plan
4. Minimize fallback complexities if changes result in an error.

What is a collection?

A user defined name that is the anchor for packages. It has not physical existence. Main usage is to group packages.

In SPUFI suppose you want to select maximum of 1000 rows, but the select returns only 200 rows. What are the 2 SQLCODEs that are returned? - GS

+100 (for successful completion of the query), 0 (for successful COMMIT if AUTOCOMMIT is set to Yes).

How would you print the output of an SQL statement from SPUFI? - GS

Print the output dataset.

Lot of updates have been done on a table due to which indexes have gone haywire. What do you do?

Looks like index page split has occurred. DO a REORG of the indexes.

What is dynamic SQL? - GS

Dynamic SQL is a SQL statement created at program execution time.

When is the access path determined for dynamic SQL? - GS

At run time, when the PREPARE statement is issued.

Suppose I have a program which uses a dynamic SQL and it has been performing well till now. Off late, I find that the performance has deteriorated. What happened? - GS

There may be one of the following reasons:
Probably RUN STATS is not done and the program is using a wrong index due to incorrect stats.
Probably RUNSTATS is done and optimizer has chosen a wrong access path based on the latest statistics.

How does DB2 store NULL physically?

As an extra-byte prefix to the column value. Physically, the null prefix is Hx '00' if the value is present and Hex 'FF' if it is not.

How do you retrieve the data from a nullable column? - GS

Use null indicators. Syntax ... INTO :HOSTVAR:NULLIND

What is the picture clause of the null indicator variable? - GS

S9(4) COMP.

What does it mean if the null indicator has -1, 0, -2? - GS

-1 : the field is null; 0 : the field is not null; -2 : the field value is truncated

How do you insert a record with a nullable column?

To insert a NULL, move -1 to the null indicator, To insert a valid value, move 0 to the null indicator