SQLRPGLE – the modern way

We’ve all been told to use embedded SQL in our RPGLE programs – it’s the modern way!  I’ve heard a lot about all the advancements in DB2 so where possible I’ve adopted SQLRPGLE as much as possible.  To be honest I do find some of the syntax a bit strange, particularly for the more advanced tools available to manage XML and web services, but it can be very powerful if used correctly!

Here is a code snippet for a simple update program, replacing all the main op-codes with SQL commands.

ctl-opt dftactgrp(*no) actgrp(*new) ; 
 
// Variables 
 dcl-s myfield char(10); 
 
// Set SQL options 
 exec sql 
 set option 
 commit = *none, datfmt = *ISO; 
 
// setLL *loval file1 
 exec sql 
 declare mycursor cursor for 
 select * from file1 
 for update of field1; 
 
 exec sql 
 open mycursor;

// read file1 
 exec sql 
 fetch mycursor into :myfield; 
 
// dow not %eof(file1) 
 dow sqlcod <> 100; 
 
// field1 = 'changed' 
// update file1r (field1) 
 exec sql 
 update file1
 set field1 = 'changed' 
 where current of mycursor; 
 
// read file1 
 exec sql 
 fetch mycursor into :myfield;
 enddo;

 close cursor; 
 
 return;