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:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework