w3resource
PL/SQL Cursor Exercises

PL/SQL Cursor Exercises: Show the uses of SQL%NOTFOUND to determine if a UPDATE statement affected any rows

PL/SQL Cursor: Exercise-6 with Solution

Write a program in PL/SQL to show the uses of SQL%NOTFOUND to determine if a UPDATE statement affected any rows.

Sample Solution:

PL/SQL Code:

DROP TABLE emp_temp;
CREATE TABLE emp_temp AS
  SELECT employee_id, first_name, last_name,email 
  FROM employees;
  
DECLARE
	z_emp_id NUMBER:=&employee_id;
BEGIN
    UPDATE emp_temp
    SET    email = 'not available'
    WHERE employee_id = z_emp_id;
 
  IF SQL%NOTFOUND THEN
      DBMS_OUTPUT.PUT_LINE ('No employee of ID '|| z_emp_id||' is found.');
  ELSE
    DBMS_OUTPUT.PUT_LINE (
      'Update succeeded for employee_id: ' || z_emp_id
    );
  END IF;
END;
/

Sample Output:

SQL> /
Enter value for employee_id: 298
old   2:        z_emp_id NUMBER:=&employee_id;
new   2:        z_emp_id NUMBER:=298;
No employee of ID 298 is found.

PL/SQL procedure successfully completed.

SQL> /
Enter value for employee_id: 157
old   2:        z_emp_id NUMBER:=&employee_id;
new   2:        z_emp_id NUMBER:=157;
Update succeeded for employee_id: 157

PL/SQL procedure successfully completed.

Flowchart:

Flowchart: PL/SQL Cursor Exercises - Show the uses of SQL%NOTFOUND to determine if a UPDATE statement affected any rows

Improve this sample solution and post your code through Disqus

Previous: Write a program in PL/SQL to show the uses of SQL%FOUND to determine if a DELETE statement affected any rows.
Next: Write a program in PL/SQL to create a table-based record using the %ROWTYPE attribute.

What is the difficulty level of this exercise?