--declare
EXEC SQL DECLARE agent_dollars CURSOR FOR
select aid, sum(dollars)
from orders
where cid = :cust_id --:cust_id is stored in host variable
group by aid
FOR READ ONLY;
--open
exec sql open agent_dollars
--fetch
exec sql whenever not found goto finish;
while (TRUE){
exec sql fetch agent_dollars into :agent_id, :dollar_sum;
printf("%s %11.2f\n", agent_id, dollar_sum);
}
finish: exec sql close agent_dollars;
--mysql
declare continue HANDLER for not found set done = true;
open cur;
fetch cur into n,c;
while(not done) do
fetch cur into n,c;
end while;
close cur;
--mysql
open cur;
read_loop:loop
fetch cur into n,c;
if done then
leave read_loop;
end if;
end loop;
close cur;
--Scrollable Cursors
EXEC SQL FETCH [{NEXT|PRIOR|FIRST|LAST} value] FROM cursor_name INTO :agent_id
--Declare Section
exec sql begin declare section;
char cust_id[5];
char cust_name[14];
float cust_discnt;
char user_name[20], user_pwd[20];
exec sql end declare section;
--Condition Handling
----Condition: SQLERROR, NOT FOUND, SQLWARNING
----Action: CONTINUE, GOTO label, STOP, DO function| BREAK | CONTINUE
exec sql whenever sqlerror goto report_error;
exec sql whenever not found goto notfound;
set discnt = :cust_discnt INDICATOR :cd_ind where cid = :cust_id;
--Connect
exec sql connect to target-server
--main
--Disconnect
exec sql commit work;
exec sql rollback work;
exec sql disconnect current;