The following examples illustrate the non-cursor Select statement:
exec sql select ename, sal
into :namevar, :salvar
from employee
where eno = :numvar;
exec sql commit;
exec sql select *
into :emprec
from employee
where eno = 23;
exec sql select 'Name: ', ename
into :title, :ename
from employee
where eno >= 148 and age = :age;
exec sql repeated select *
into :emprec
from employee
where eno = :numvar and ename = :namevar;
error = 0;
exec sql insert into employee
select * from newhires;
exec sql select eno, ename, eage, esal, dname
into :eno, :ename, :eage, :esal, :dname
from employee e, dept d
where e.edept = d.deptno
group by ename, dname
exec sql begin;
generate report of information;
if error condition then
error = 1;
exec sql endselect;
end if;
exec sql end;
/*
** Control transferred here by completing the
** retrieval or because the endselect statement
** was issued.
*/
if error = 1
print 'Error encountered after row',
sqlca.sqlerrd(3);
exec sql rollback;
else
print 'Successful addition and reporting';
exec sql commit;
end if;
run forms in query mode;
construct search_condition of employees;
exec sql select *
into :emprec
from employee
where :search_condition;
exec sql begin;
load emprec into a table field;
exec sql end;
display table field for browsing;
The main program:
...
exec sql include sqlca;
exec sql begin declare section;
/* Include output of dclgen for declaration of
** record order_rec */
exec sql include 'decls';
exec sql end declare section;
exec sql connect customers session 1;
exec sql connect sales session 2;
...
exec sql select * into :order_rec from orders;
exec sql begin;
if (order_rec.new_customer = 1) then
call new_customer(order_rec);
endif
process order;
exec sql end;
...
exec sql disconnect;
The subroutine, new_customer, which is from the select loop, contains the session switch:
subroutine new_customer(record order_rec)
begin;
exec sql set_sql(session = 1);
exec sql insert into accounts
values (:order_rec);
process any errors;
exec sql set_sql(session = 2);
/* Reset status information before resuming
** select loop */
sqlca.sqlcode = 0;
sqlca.sqlwarn.sqlwarn0 = ' ';
end subroutine;