Sample embedded SQL C fragment

This code fragment sets the current connection, and accesses data stored on Adaptive Server:


EXEC SQL INCLUDE sqlca; 
int rcode; 

EXEC SQL BEGIN DECLARE SECTION; 
char name[15]; 
char supplier[30]; 
char supplier_address[30]; 
int order_quantity; 

EXEC SQL END DECLARE SECTION; 
main() 
{ 
char errmsg[400]; 
char qmsg[400]; 
short mlen; 

EXEC SQL WHENEVER SQLERROR GOTO :errexit; 
EXEC SQL WHENEVER SQLWARNING GOTO :errexit 
EXEC SQL WHENEVER NOT FOUND GOTO :errexit 

/* Get addressability for EIB... */ 

/* 
** Write record to CICS temporary storage queue... 
*/ 

/* Send the first map */ 

EXEC CICS SEND MAP("PANEL1") MAPSET("UXA1") 
FREEKB ERASE RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X001"); 

/* Receive the response */ 

EXEC CICS RECEIVE MAP("PANEL1") MAPSET("UXA1") 
RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X002"); 

/* Select a record from the table based on user input. */ 

sprintf(name, "%s", panel1.panel1i.newnamei); 
EXEC SQL SET CONNECTION connection_1; 
EXEC SQL SELECT name, supplier, supplier_address,order_quantity 
INTO 
:name, :supplier, :supplier_address, :order_quantity 
FROM cheese 
WHERE name = :name; 

/* Handle "no rows returned" from SELECT */ 

if (sqlca.sqlcode == 100) 
{ 
sprintf(panel4.panel4o.messageo, "%s", 
NOCHEESE); 
EXEC CICS SEND MAP("PANEL4") MAPSET("UXA1") FREEKB ERASE RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X009"); 

EXEC CICS SEND CONTROL FREEKB; 
EXEC CICS RETURN; 

}
/* Fill in and send the second map */ 

memset ( &panel2.panel2o, ’0’, 
sizeof(panel2.panel2o)); 
sprintf(panel2.panel2o.nameo, "%s", name); 
sprintf(panel2.panel2o.supplo, "%s",supplier); 
sprintf(panel2.panel2o.addresso, "%s", 
supplier_address); 
sprintf(panel2.panel2o.ordero, "%d", order_quantity); 

EXEC CICS SEND MAP("PANEL2") MAPSET("UXA1") 
FREEKB ERASE RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X003"); 

/* Receive the response */ 

if (panel2.panel2i.questi == ’y’) 

{

/* Send the third map... */ 
/* Receive the response... */ 
/* Update the database */ 

order_quantity = atoi(panel3.panel3i.newordi); 

EXEC SQL UPDATE cheese 
set order_quantity = :order_quantity 
where name = :name; 

/* Write a record to the temporary queue */ 

sprintf(qmsg, "%s", "The cheese table was updated"); 
mlen = strlen(qmsg); 


EXEC CICS WRITEQ TS QUEUE("TEMPXAQ1") 
FROM(qmsg) LENGTH(mlen) RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X010"); 
} 
else 
{ 

/* 
** The user does not wish to update so free the keyboard and return... 
*/ 
} 
/* Commit the update */ 

EXEC CICS SYNCPOINT RESP(rcode); 
if (rcode != DFHRESP(NORMAL)) 
EXEC CICS ABEND ABCODE("X011"); 

/* 
** Send the fourth map confirming successful update... 
*/ 

EXEC CICS RETURN; 
errexit: 
fprintf(stderr,"error in cheeseland %d\n",sqlca.sqlcode); 

/* Handle general errors */ 

sprintf(errmsg, "%.60s\n",sqlca.sqlerrm.sqlerrmc); 
strncpy(panel4.panel4o.messageo, errmsg, 60); 
sprintf(panel4.panel4o.codeo, "%d", sqlca.sqlcode); 

/* 
** Send the fourth map with appropriate message... 
*/ 

/* Rollback the transaction */ 

EXEC CICS SYNCPOINT ROLLBACK; 
EXEC CICS SEND CONTROL FREEKB; 
EXEC CICS RETURN; 
 }