Saturday, 24 August 2013

DB2 Host Variables and Structures

What are DB2 Host Variables and Structures?

DB2 Host Variables and Structures are a powerful feature that allows you to store data in the database server, rather than in your application. This can be very useful for storing frequently used values, such as connection strings or table names.
Benefits of using DB2 Host Variables and Structures:

  • They allow you to store data on the database server instead of in your application code. This saves space on your computer's hard drive and reduces network traffic between client machines and servers because only one copy needs to be sent over the network instead of multiple copies being sent separately by each user connected at any given time (this is especially true if they're working with large files). It also makes updating easier because changes made by one user won't affect other users who may still need access their own copies until they update theirs accordingly; this avoids having multiple versions floating around out there somewhere!

Types of DB2 Host Variables and Structures

There are several types of DB2 host variables and structures.

  • Character Host Variables: A variable that contains character data, such as the name of a database table or column, can be declared as a character variable.

  • Numeric Host Variables: The numeric type is used to store integer values in an application program. You can also use this data type for floating-point numbers that have no decimal places (such as 0).

  • Logical Host Variables: This data type stores Boolean values--either true or false--and is often used in conditional statements such as IF statements in languages such as COBOL or PL/I.

  • Date Host Variables: Date variables are used for storing dates in your application programs so that they can be manipulated easily by using built-in functions such as TODAY() which returns today's date at midnight when executed on any given day regardless of time zone differences across different regions around the world where DB2 might be deployed

Creating DB2 Host Variables and Structures

  • Declaring Host Variables
    To declare a host variable, you must use the DECLARE statement. The syntax for this statement is:
    DECLARE <host-variable-name> [<data type>] [VARYING] [NOT NULL] <host-variable-name> is the name of your new host variable. You can use any valid SQL identifier as a host variable name (for example, VAR1). <data type> specifies the data type of your new host variable; this can be any valid DB2 data type except BOOLEAN or CHARACTER SET. If you omit <data type>, then DB2 assumes that it's an integer (INTEGER). If you want to explicitly specify another numeric data type such as DECIMAL(5), then add it after "INTEGER" in brackets like so: DECLARE My_New_Host_Variable INTEGER(5) NOT NULL

Using DB2 Host Variables and Structures

Host variables can be used in SELECT statements, INSERT statements, UPDATE statements and DELETE statements.

  • In a SELECT statement:

    • To return the value of a host variable from a table or view you must use an expression that contains the name of the host variable. This can be done using either column names or aliases for columns. For example:
      SELECT * FROM myview WHERE MYVAR = 'myvalue';

Common DB2 Host Variables and Structures Examples

Here are some examples of the most common host variables and structures:

  • Character Host Variables
    Character host variables are used to store character data. They can be used in SQL statements, stored procedures, and triggers. For example:
    CHARACTER(10) VARCHAR(10) = 'Hello World';
    This statement creates a variable named VARCHAR with a length of 10 characters. The value 'Hello World' is assigned to it using single quotes ('). It's important to note that if you do not specify the length when defining your variable, then DB2 will automatically assign it one byte less than its maximum allowed size (for example: CHARACTER(1) becomes CHARACTER(0)). Also remember that if you want spaces between words then use double quotes (").

Best Practices for DB2 Host Variables and Structures

Best Practices for DB2 Host Variables and Structures

  • Use appropriate data types. If you know that a variable will only contain numbers, then use an integer type. Similarly, if you know that a variable will only contain text (i.e., character) values, then use a character type. This will help minimize the amount of space used by your host variables/structures and improve performance by avoiding unnecessary conversions between different types of data during execution time.

  • Minimize the number of host variables used in each program unit by passing arguments instead where possible or using global variables or constants instead when appropriate (i.e., when not passing arguments).

  • Minimize structure usage wherever possible by implementing C-style arrays instead (see below). When using structures:

    • Use null values whenever possible; this allows you to avoid having to declare many different structures for each value type (e.g., one structure per Boolean value).

Debugging DB2 Host Variables and Structures

Debugging DB2 Host Variables and Structures
The following sections describe how to debug DB2 host variables and structures:
-Checking Data Types
-Checking Host Variable Values
-Checking Structure Values
-Checking for Null Values

Security Considerations for DB2 Host Variables and Structures

You can use host variables to store data that is passed from the client to the server and vice versa. When you use a host variable, the client application passes its value through an SQL statement and sends it to DB2 as part of a query. The DB2 server then stores this value for later retrieval by other applications or users.
When using host variables, there are several considerations for security:

  • Securely storing host variable values can be difficult because they may contain sensitive information such as user IDs or passwords. If you're not careful when designing your database schema, you could expose sensitive information in plain text on disk--which would make it easier for someone who has access to your system (or even just read-only access) to see what's going on inside your database! To avoid this problem altogether we recommend encrypting any values stored in host variables with encryption keys generated by DB2 itself; these keys are known only by DB2 itself so no one else will know how they work except those who designed them!


A host variable is a data item declared in the host language for use within an SQL statement. Using host variables, you can
  • Retrieve data into the host variable for your application program's use
  • Place data into the host variable to insert into a table or to change the contents of a row
  • Use the data in the host variable when evaluating a WHERE or HAVING clause
  • Assign the value in the host variable to a special register, such as CURRENT SQLID and CURRENT DEGREE
  • Insert null values in columns, using a host indicator variable that contains a negative value
  • Use the data in the host variable in statements that process dynamic SQL, such as EXECUTE, PREPARE, and OPEN  
A host structure is a group of host variables that an SQL statement can refer to using a single name. You can use host structures in all languages except REXX.
Use the host language statements to define the host structures. In some languages, such as C, the program variables that are used as host variables must be defined in a section of the program that is delimited by BEGIN DECLARE SECTION and END DECLARE SECTION statements, as follows:
   EXEC SQL BEGIN DECLARE SECTION;
   DECLARE DB2USER1.TEST_TABLE
       CHAR    NUMBER[7];
       CHAR    NAME[51];
       SHORT LENGTH;
   EXEC SQL END DECLARE SECTION;




Created with Artisteer

No comments:

Post a Comment

New In-feed ads