DB2 Insert Example

   

  Asked by SQL - DB2 Insert Example:
The following show you a sample SQL DB2 insert. This shows example code assuming you were inserting into a DB2 table that was named "SAMPTAB", where it has 3 columns in the table each defined differently.  One is an integer column, one is a decimal column, and one is a 5 byte char column as shown.

Although this sample is showing a mainframe DB2 SQL sample, these are mostly the same everywhere so the platform doesnt really matter much.

INSERT INTO SAMPTAB
VALUES (5, 17.2, 'TEST1');


Anonymous Says:
I would format the insert better though course, usually you have more columns.  Format it like this in your cobol code.

EXEC SQL
  INSERT INTO SAMPTAB
  (COL_1
  ,COL_2
  ,COL_3
  ,COL_4)
VALUES
  (5
  ,17.2
  , 'TEST1'
  ,:HOST_VAR4)
END-EXEC

EVALUATE SQLCODE
   WHEN +0
      CONTINUE
   WHEN OTHER
      DISPLAY 'ERROR in paragrah etc .... "
      MOVE SQLCODE TO WS-SQL-DISPLAY
      DISPLAY 'SQLCODE is ' WS-SQL-DISPLAY
      .... Do your error logic
END-EVALUATE

In working storage WS-SQL-DISPLAY is defined as PIC -ZZZ9.

Anonymous Says:
Just showing a host variable but normally I would make all fields db2 dclgen host vars of course and populate fields in the db2 dclgen fields for inserting.

Anonymous Says:
Good idea on always using the dclgen fields for this, that guarantees you never have incompatible format issues for certain db2 columns.  Thats probably a good standard to always enforce in any new programs.

Add your reply below ...


Your Nickname:

Your Reply:

Type the code: