5. Working with Embedded SQL : Data Handlers for Large Objects : Example: GET DATA Handler
 
Share this page                  
Example: GET DATA Handler
The following example illustrates the use of the GET DATA statement in a data handler. This routine retrieves a chapter titled, “One Dark and Stormy Night,” from the book table which has the following structure.
exec sql create table book
     (chapter_name char(50),
      chapter_text long varchar);
The data handler routine is called when the select statement is executed:
exec sql begin declare section;
 
          char     chapter_namebuf(50);
 
exec sql end declare section;
 
          int      get_handler()
 
...
 
Copy the string "One Dark and Stormy Night" into the chapter_namebuf variable.
exec sql select chapter_name, chapter_text
     into :chapter_namebuf, datahandler(get_handler())
     from book where chapter_name = :chapter_namebuf
     exec sql begin
          /* get_handler will be invoked
          once for each row */
     exec sql end;
...
 
get_handler()
 
exec sql begin declare section;
     char         chap_segment[1000];
     int          segment_length;
     int          data_end;
     int          error;
exec sql end declare section;
 
          ...
 
exec sql whenever sqlerror goto err;
 
data_end = 0
 
open file for writing
 
/* retrieve 1000 bytes at a time and write to text file. on last segment, less than 1000 bytes may be  returned, so segment_length is used
for actual number of bytes to write to file. */
 
while (data_end != 1)
 
     exec sql get data (:chap_segment = segment,
          :segment_length = segmentlength,
          :data_end = dataend)
          with maxlength = 1000;
 
     write segment_length number of bytes from
     "chap_segment" to text file
 
end while
 
...
 
err:
 
exec sql inquire_ingres(:error = errorno);
 
if (error != 0)
 
     print error
     close file