Was this helpful?
Example: PUT DATA Handler
The following example illustrates the use of the PUT DATA statement; the data handler routine writes a chapter from a text file to the book table. The data handler is called when the INSERT statement is executed on a table with the following structure.
exec sql create table book
     (chapter_name char(50),
      chapter_text long varchar);
For example:
exec sql begin declare section;
          char chapter_namebuf(50);
exec sql end declare section;
 
int put_handler();/* not necessary to
                    declare to embedded SQL */
...
copy chapter text into chapter_namebuf
 
exec sql insert into book
     (chapter_name, chapter_text)
      values (:chapter_namebuf,
      datahandler(put_handler()));
...
 
put_handler()
 
exec sql begin declare section;
          char         chap_segment[3000];
          int          chap_length;
          int          segment_length;
          int          error;
 
exec sql end declare section;
 
int               local_count = 0;
 
               ...
exec sql whenever sqlerror goto err;
 
chap_length = byte count of file
 
open file for reading
 
loop while (local_count < chap_length)
 
          read segment from file into chap_segment
 
          segment_length = number of bytes read
 
          exec sql put data
          (segment = :chap_segment,
           segmentlength = :segment_length)
 
     local_count = local_count + segment_length
 
end loop
 
exec sql put data (dataend = 1); /* required by embedded SQL */
 
...
 
err:
 
exec sql inquire_sql(:error = errorno);
 
if (error <> 0)
     print error
     close file
Last modified date: 11/28/2023