Example: Coding with row IDs for direct row access
You can obtain the row ID value for a row, and then to use that value to find the row efficiently when you want to modify it.
The following figure is a
portion of a C program that shows you how to obtain the row ID value
for a row and use that value to find the row efficiently.
/**************************/
/* Declare host variables */
/**************************/
EXEC SQL BEGIN DECLARE SECTION;
SQL TYPE IS BLOB_LOCATOR hv_picture;
SQL TYPE IS CLOB_LOCATOR hv_resume;
SQL TYPE IS ROWID hv_emp_rowid;
short hv_dept, hv_id;
char hv_name[30];
decimal hv_salary[5,2];
EXEC SQL END DECLARE SECTION;
/**********************************************************/
/* Retrieve the picture and resume from the PIC_RES table */
/**********************************************************/
strcpy(hv_name, "Jones");
EXEC SQL SELECT PR.PICTURE, PR.RESUME INTO :hv_picture, :hv_resume
FROM PIC_RES PR
WHERE PR.Name = :hv_name;
/**********************************************************/
/* Insert a row into the EMPDATA table that contains the */
/* picture and resume you obtained from the PIC_RES table */
/**********************************************************/
EXEC SQL INSERT INTO EMPDATA
VALUES (DEFAULT,9999,'Jones', 35000.00, 99,
:hv_picture, :hv_resume);
/**********************************************************/
/* Now retrieve some information about that row, */
/* including the ROWID value. */
/**********************************************************/
hv_dept = 99;
EXEC SQL SELECT E.SALARY, E.EMP_ROWID
INTO :hv_salary, :hv_emp_rowid
FROM EMPDATA E
WHERE E.DEPTNUM = :hv_dept AND E.NAME = :hv_name;
/**********************************************************/
/* Update columns SALARY, PICTURE, and RESUME. Use the */
/* ROWID value you obtained in the previous statement */
/* to access the row you want to update. */
/* smiley_face and update_resume are */
/* user-defined functions that are not shown here. */
/**********************************************************/
EXEC SQL UPDATE EMPDATA
SET SALARY = :hv_salary + 1200,
PICTURE = smiley_face(:hv_picture),
RESUME = update_resume(:hv_resume)
WHERE EMP_ROWID = :hv_emp_rowid;
/**********************************************************/
/* Use the ROWID value to obtain the employee ID from the */
/* same record. */
/**********************************************************/
EXEC SQL SELECT E.ID INTO :hv_id
FROM EMPDATA E
WHERE E.EMP_ROWID = :hv_emp_rowid;
/**********************************************************/
/* Use the ROWID value to delete the employee record */
/* from the table. */
/**********************************************************/
EXEC SQL DELETE FROM EMPDATA
WHERE EMP_ROWID = :hv_emp_rowid;