IBM Books

Data Movement Utilities Guide and Reference


Example Load Sessions

CLP Examples

Example 1

TABLE1 has 5 columns:

ASCFILE1 has 6 elements:

Data Records:

   1...5....10...15...20...25...30...35...40
   Test data 1         XXN 123abcdN
   Test data 2 and 3   QQY    wxyzN
   Test data 4,5 and 6 WWN6789    Y

The following command loads the table from the file:

   db2 load from ascfile1 of asc modified by striptblanks reclen=40
      method L (1 20, 21 22, 24 27, 28 31)
      null indicators (0,0,23,32)
      insert into table1 (col1, col5, col2, col3)

Notes:

  1. The specification of striptblanks in the MODIFIED BY parameter forces the truncation of blanks in VARCHAR columns (COL1, for example, which is 11, 17 and 19 bytes long, in rows 1, 2 and 3, respectively).

  2. The specification of reclen=40 in the MODIFIED BY parameter indicates that there is no new-line character at the end of each input record, and that each record is 40 bytes long. The last 8 bytes are not used to load the table.

  3. Since COL4 is not provided in the input file, it will be inserted into TABLE1 with its default value (it is defined NOT NULL WITH DEFAULT).

  4. Positions 23 and 32 are used to indicate whether COL2 and COL3 of TABLE1 will be loaded NULL for a given row. If there is a Y in the column's null indicator position for a given record, the column will be NULL. If there is an N, the data values in the column's data positions of the input record (as defined in L(........)) are used as the source of column data for the row. In this example, neither column in row 1 is NULL; COL2 in row 2 is NULL; and COL3 in row 3 is NULL.

  5. In this example, the NULL INDICATORS for COL1 and COL5 are specified as 0 (zero), indicating that the data is not nullable.

  6. The NULL INDICATOR for a given column can be anywhere in the input record, but the position must be specified, and the Y or N values must be supplied.

Example 2 (Loading LOBs from Files)

TABLE1 has 3 columns:

ASCFILE1 has 3 elements:

The following files reside in either /u/user1 or /u/user1/bin:

Data Records in ASCFILE1:

   1...5....10...15...20...25...30.
   REC1 ASCFILE2 ASCFILE3
   REC2 ASCFILE4 ASCFILE5
   REC3 ASCFILE6 ASCFILE7

The following command loads the table from the file:

   db2 load from ascfile1 of asc
      lobs from /u/user1, /u/user1/bin
      modified by lobsinfile reclen=22
      method L (1 4, 6 13, 15 22)
      insert into table1

Notes:

  1. The specification of lobsinfile in the MODIFIED BY parameter tells the loader that all LOB data is to be loaded from files.

  2. The specification of reclen=22 in the MODIFIED BY parameter indicates that there is no new-line character at the end of each input record, and that each record is 22 bytes long.

  3. LOB data is contained in 6 files, ASCFILE2 through ASCFILE7. Each file contains the data that will be used to load a LOB column for a specific row. The relationship between LOBs and other data is specified in ASCFILE1. The first record of this file tells the loader to place REC1 in COL1 of row 1. The contents of ASCFILE2 will be used to load LOB1 of row 1, and the contents of ASCFILE3 will be used to load LOB2 of row 1. Similarly, ASCFILE4 and ASCFILE5 will be used to load LOB1 and LOB2 of row 2, and ASCFILE6 and ASCFILE7 will be used to load the LOBs of row 3.

  4. The LOBS FROM parameter contains 2 paths that will be searched for the named LOB files when those files are required by the loader.

  5. To load LOBs directly from ASCFILE1 (a non-delimited ASCII file), without the lobsinfile modifier, the following rules must be observed:

Example 3 (Using Dump Files)

Table FRIENDS is defined as:

   table friends "( c1 INT NOT NULL, c2 INT, c3 CHAR(8) )"

If an attempt is made to load the following data records into this table,

   23, 24, bobby
   , 45, john
   4,, mary

the second row is rejected because the first INT is NULL, and the column definition specifies NOT NULL. Columns which contain initial characters that are not consistent with the DEL format will generate an error, and the record will be rejected. Such records can be written to a dump file (see Table 8).

DEL data appearing in a column outside of character delimiters is ignored, but does generate a warning. For example:

   22,34,"bob"
   24,55,"sam" sdf

The utility will load "sam" in the third column of the table, and the characters "sdf" will be flagged in a warning. The record is not rejected. Another example:

   22 3, 34,"bob"

The utility will load 22,34,"bob", and generate a warning that some data in column one following the 22 was ignored. The record is not rejected.

Example 4 (Loading DATALINK Data)

The following command loads the table MOVIETABLE from the input file delfile1, which has data in the DEL format:

   db2 load from delfile1 of del
       modified by dldel|
       insert into movietable (actorname, description, url_making_of, url_movie)
       datalink specification (dl_url_default_prefix "http://narang"),
       (dl_url_replace_prefix "http://bomdel" dl_url_suffix ".mpeg")
       for exception excptab

Notes:

  1. The table has four columns:
       actorname              VARCHAR(n)
       description            VARCHAR(m)
       url_making_of          DATALINK (with LINKTYPE URL)
       url_movie              DATALINK (with LINKTYPE URL)
    

  2. The DATALINK data in the input file has the vertical bar (|) character as the sub-field delimiter.

  3. If any column value for url_making_of does not have the prefix character sequence, "http://narang" is used.

  4. Each non-NULL column value for url_movie will get "http://bomdel" as its prefix. Existing values are replaced.

  5. Each non-NULL column value for url_movie will get ".mpeg" appended to the path. For example, if a column value of url_movie is "http://server1/x/y/z", it will be stored as "http://bomdel/x/y/z.mpeg"; if the value is "/x/y/z", it will be stored as "http://bomdel/x/y/z.mpeg".

  6. If any unique index or DATALINK exception occurs while loading the table, the affected records are deleted from the table and put into the exception table excptab.

API Examples

The following sample program shows how to:

For detailed information about the SAMPLE database, see the Administration Guide.

The source file for this sample program (tload.sqc) can be found in the \sqllib\samples\c directory. It contains both DB2 APIs and embedded SQL calls. The script file bldvaemb.cmd, located in the same directory, contains the commands to build this and other sample programs. For general information about creating applications containing DB2 administrative APIs, and detailed information about compile and link options, see the Application Building Guide. To build the sample program tload from the source file tload.sqc on OS/2:

  1. Copy the files tload.sqc, bldvaemb.cmd, util.c, and util.h to a working directory.

  2. If the database manager is not running, issue the command db2start.

  3. Enter bldvaemb tload sample. The following files are generated:
       tload.bnd
       tload.c
       util.obj
       tload.obj
       tload.exe
    

To run the sample program (executable file), enter tload. You might find it useful to examine some of the generated files, such as the message files, and the delimited ASCII data file.

 
 
/******************************************************************************
**
** Source File Name = tload.sqc  1.4
**
** Licensed Materials - Property of IBM
**
** (C) COPYRIGHT International Business Machines Corp. 1995, 1997 
** All Rights Reserved.
**
** US Government Users Restricted Rights - Use, duplication or
** disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
**
**
**    PURPOSE :
**      To show the use of the QUIESCE TABLESPACE and the LOAD APIs.
**        - EXPORT the EMP_RESUME table into a comma delimited file.
**        - create a temporary table ('loadtable').
**        - QUIESCE the TABLESPACES, preparing the temporary table to be 
**          LOADable.
**        - LOAD the comma delimited file into a temporary table ('loadtable').
**
**    STRUCTURES USED :
**       sqldcol
**       sqlchar
**       sqluexpt_out
**       sqlca
**
**    APIs USED :
**       EXPORT                           sqluexpr
**       QUIESCE TABLESPACE FOR TABLES    sqluvqdp
**       LOAD                             sqluload
**
**    FUNCTIONS DECLARED :
**       'C' COMPILER LIBRARY :
**          stdio.h  -  printf
**          string.h -  fgets, strncpy
**
**       DBMS LIBRARY :
**          sqlenv.h -  see "APIs USED" above
**
**       OTHER :
**          external :
**             check_error :     Checks for SQLCODE error, and prints out any
**             [in UTIL.C]       related information available.
**
**    EXTERNAL DEPENDANCIES :
**       - Ensure existence of database (SAMPLE) for precompile purposes.
**       - Precompile with the SQL precompiler (PREP in DB2)
**       - Bind to a database (BIND in DB2)
**       - Compile and link with the IBM Cset++ compiler (AIX and OS/2)
**         or the Microsoft Visual C++ compiler (Windows) 
**         or the compiler supported on your platform.
**
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlenv.h>
#include <sqlutil.h>
#include <malloc.h>
 
#define  CHECKERR(CE_STR)   if (check_error (CE_STR, &sqlca) != 0) return 1;
#ifdef DB2AIX
#define  WORKDIR  "/u/workdir"
#else 
#define WORKDIR "."
#endif
 
EXEC SQL INCLUDE SQLCA;
int main (int argc, char *argv[]) {
   short int            callerAction = 0;
   struct sqldcol       DataDescriptor;
   struct sqlchar       *ActionString;
   struct sqlchar       *FileTypeMod;
   struct sqluexpt_out  outputInfo;
   char        datafile[] = "EXPTABLE.DEL";
   char        statement[] = "SELECT empno, photo_format, picture FROM emp_photo";
   char        impStatement[] = "INSERT INTO loadtable (num, format, photo)";
/*
   char        statement[] = "SELECT empno, photo_format FROM emp_photo";
   char        impStatement[] = "INSERT INTO loadtable (num, format)";
*/
 
   char        msgfile_x[] = "EXPMSG.TXT";
   char        FileType[] = SQL_DEL;
 
 
   char        table_name[18];
 
   /* Variables for the LOAD API */
   struct sqlu_media_list       DataFileList;
   struct sqlu_media_list       *pLobPathList;
   struct sqluload_in           InputInfo;
   struct sqluload_out          OutputInfo;
   struct sqlu_media_list       *pWorkDirectoryList;
   struct sqlu_media_list       *pCopyTargetList;
   char                         LocalMsgFileName[] = "LOADMSG";
   char                         RemoteMsgFileName[] = "RLOADMSG";
   short                        CallerAction;
   long                         *pNullIndicators;
   void                         *pReserved;
 
 
   EXEC SQL BEGIN DECLARE SECTION;
      char userid[9];
      char passwd[19];
   EXEC SQL END DECLARE SECTION;
 
   printf ("This is sample program 'tload.sqc'\n");
 
   /* need to preset the size of structure field and counts */
   outputInfo.sizeOfStruct = SQLUEXPT_OUT_SIZE;
 
   /******************************************************************\
   * need to allocate the proper amount of space for the SQL statement *
   \******************************************************************/
   ActionString = (struct sqlchar *)malloc(strlen(statement)
      + sizeof (struct sqlchar));
   ActionString->length = strlen(statement);
   strncpy (ActionString->data, statement, strlen(statement));
 
   FileTypeMod = (struct sqlchar *)malloc(strlen("lobsinfile")
      + sizeof (struct sqlchar));
   FileTypeMod->length = strlen("lobsinfile");
   strncpy (FileTypeMod->data, "lobsinfile", FileTypeMod->length);
 
   /* DELimited format can not have specified names, therefore the
      column method is 'D'efault */
   DataDescriptor.dcolmeth = SQL_METH_D;
 
   if (argc == 1) {
      EXEC SQL CONNECT TO sample;
	  CHECKERR ("CONNECT TO SAMPLE");
   }
   else if (argc == 3) { 
      strcpy (userid, argv[1]);
      strcpy (passwd, argv[2]);
      EXEC SQL CONNECT TO sample USER :userid USING :passwd; 
      CHECKERR ("CONNECT TO SAMPLE");
   }
   else {
      printf ("\nUSAGE: tload [userid passwd]\n\n");
      return 1;
   } /* endif */
   
   printf ("Exporting EMP_RESUME table into file '%s'\n", datafile);
   /*******************\
   * EXPORT API called *
   \*******************/
   sqluexpr (datafile, NULL, NULL, &DataDescriptor, ActionString,
      FileType, FileTypeMod, msgfile_x, 0, &outputInfo, NULL, &sqlca);
   CHECKERR ("exporting table");
   printf ("Rows exported %d\n", outputInfo.rowsExported);
 
 
   free (ActionString);
 
   /* need to allocate the proper amount of space for the SQL statement */
   ActionString = (struct sqlchar *)malloc(strlen(impStatement)
      + sizeof (struct sqlchar));
   ActionString->length = strlen(impStatement);
   strncpy (ActionString->data, impStatement, strlen(impStatement));
 
   printf ("Creating a temporary table 'loadtable' to load into\n");
   /* create a temporary table to import into */
   EXEC SQL CREATE TABLE loadtable (num CHARACTER(6), format VARCHAR(10),
      photo BLOB(100K));
   CHECKERR ("CREATE TABLE");
 
   /* end the transaction so the program can quiesce the tablespace */
   EXEC SQL COMMIT;
 
   printf ("Quiescing tablespaces for table 'loadtable'\n");
   /******************************\
   * QUIESCE TABLESPACE FOR TABLE *
   \******************************/
   sqluvqdp ("loadtable", SQLU_QUIESCEMODE_EXCLUSIVE, NULL, &sqlca);
   CHECKERR ("QUIESCE TABLESPACES FOR TABLE");
 
 
   printf ("Loading the file '%s' into 'loadtable'\n", datafile);
 
   /* initializing the variables for the LOAD API */
   /* the DataFileList structure */
   DataFileList.media_type = SQLU_SERVER_LOCATION;
   DataFileList.sessions = 1;
   DataFileList.target.location = (sqlu_location_entry *) malloc
      (sizeof(sqlu_location_entry) * DataFileList.sessions);
   strcpy (DataFileList.target.location->location_entry, datafile);
 
   pLobPathList = NULL;
   CallerAction = SQLU_INITIAL;
 
   /* the sqluload input structure */
   InputInfo.sizeOfStruct = SQLULOAD_IN_SIZE; /* this should never change */
   InputInfo.savecnt = 1;                     /* consistency points as frequent */
                                              /*   as possible */
   InputInfo.restartcnt = 0;                  /* start at row 1 */
   InputInfo.rowcnt = 0;                      /* load all rows */
   InputInfo.warningcnt = 0;                  /* don't stop for warnings */
   InputInfo.data_buffer_size = 0;            /* default data buffer size */
   InputInfo.sort_buffer_size = 0;            /* default warning buffer size */
   InputInfo.hold_quiesce = 0;                /* don't hold the quiesce */
   InputInfo.restartphase = ' ';              /* ignored anyway, but must be ' ',L,B,D */
   InputInfo.statsopt = SQLU_STATS_NONE;      /* don't bother collecting them */
 
   /* the sqluload output structure */
   OutputInfo.sizeOfStruct = SQLULOAD_OUT_SIZE;
 
   /* the CopyTargetList structure */
 
      pCopyTargetList = NULL;
 
   OutputInfo.sizeOfStruct = SQLULOAD_OUT_SIZE;
   /******\
   * LOAD *
   \******/
   sqluload (&DataFileList,
             pLobPathList,
             &DataDescriptor,
             ActionString,
             FileType,
             FileTypeMod,
             LocalMsgFileName,
             RemoteMsgFileName,
             CallerAction,
             &InputInfo,
             &OutputInfo,
             pWorkDirectoryList,
             pCopyTargetList,
             pNullIndicators,
             pReserved,
             &sqlca);
   CHECKERR ("LOADing table");
 
   printf ("Rows loaded %d\nrows committed %d\n", OutputInfo.rowsLoaded,
      OutputInfo.rowsCommitted);
 
   free (ActionString);
 
   /* drop the table  */
   EXEC SQL DROP TABLE loadtable;
   
   EXEC SQL CONNECT RESET;
   CHECKERR ("CONNECT RESET");
}
/* end of program : tload.sqc */
 

The source file for a sample program called loadqry.sqc can be found in the \sqllib\samples\c directory. This sample program shows how to use an API to query the current status of a load operation against a database to which the program is connected. It contains both DB2 APIs and embedded SQL calls. The script file bldvaemb.cmd, located in the same directory, contains the commands to build this and other sample programs. For general information about creating applications containing DB2 administrative APIs, and detailed information about compile and link options, see the Application Building Guide. To build the sample program loadqry from the source file loadqry.sqc on OS/2:

  1. Copy the files loadqry.sqc, bldvaemb.cmd, util.c, and util.h to a working directory.

  2. If the database manager is not running, issue the command db2start.

  3. Enter bldvaemb loadqry sample. The following files are generated:
       loadqry.bnd
       loadqry.c
       util.obj
       loadqry.obj
       loadqry.exe
    

To run the sample program (executable file), enter loadqry. You might find it useful to examine the message file. This file will contain information only if the program is run when there is a load operation in progress.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]