Declaration of table and view definitions

Declaring table or view definitions is optional, but they offer several advantages. You can declare a table or view by including an SQL DECLARE statement in your program.

Before your program issues SQL statements that retrieve, update, delete, or insert data, you must declare the tables and views that your program accesses. Declaring tables or views is not required; however, declaring them offers advantages such as documenting application programs and providing the precompiler with information that is used to check your embedded SQL statements.

Example

Begin general-use programming interface information.

The DECLARE TABLE statement (written in COBOL) for the DEPT table looks like the following example:

EXEC SQL
  DECLARE DEPT TABLE
    (DEPTNO    CHAR(3)           NOT NULL,
     DEPTNAME  VARCHAR(36)       NOT NULL,
     MGRNO     CHAR(6)                   ,
     ADMRDEPT  CHAR(3)           NOT NULL )
END-EXEC.
End general-use programming interface information.

For each traditional language, you delimit an SQL statement in your program between EXEC SQL and a statement terminator. In the preceding example, the EXEC SQL and END-EXEC delimit the SQL statement in a COBOL program.

As an alternative to coding the DECLARE statement yourself, you can use the Db2 subcomponent DCLGEN, the declarations generator.