This Study Resource Was: Database Programming With PL/SQL 8-1: Practice Activities
This Study Resource Was: Database Programming With PL/SQL 8-1: Practice Activities
This Study Resource Was: Database Programming With PL/SQL 8-1: Practice Activities
com/academy
Subprograms Named PL/SQL blocks that are compiled and stored in the
database.
m
er as
Anonymous Blocks Unnamed executable PL/SQL blocks that cannot be reused or
co
eH w
stored in the database for later use.
o.
Procedures Named PL/SQL blocks that can accept parameters and are
rs e compiled and stored in the database.
ou urc
Try It / Solve It
o
CODE SAMPLE A
ed d
ar stu
DECLARE
v_empid employees.employee_id%TYPE := 100;
is
UPDATE employees
SET salary = (salary * v_percent_increase) + salary
sh
CODE SAMPLE B
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
This study source was downloaded by 100000814899389 from CourseHero.com on 04-21-2021 22:31:18 GMT -05:00
https://www.coursehero.com/file/41371892/PLSQL-8-1-Practicedocx/
2
m
er as
One of them is a stored procedure and the other is an anonymous block, the stored procedure can be
co
eH w
called at anytime by using BEGIN and END, while the anonymous block can only be used as it is
created.
o.
rs e
2. In your own words, list the benefits of subprograms.
ou urc
They can be used whenever, have names, only need to be created once, can take parameters, and
they are stored in the database.
o
A stored procedure is a named PL/SQL block that can accept parameters and is stored in the
vi y re
A. Use the code below to create a procedure in Application Express. Save the definition of your
procedure in case you need to modify it later. In the “Save SQL” popup, name your saved work “My
sh
https://www.coursehero.com/file/41371892/PLSQL-8-1-Practicedocx/
3
BEGIN
name_change;
END;
C. SELECT from the table to check that the procedure has executed correctly and performed the
UPDATE.
m
er as
SELECT *
co
FROM employees_dup
eH w
WHERE department_id = 80;
o.
rs e
5. Create a second procedure named pay_raise which changes the salary of all employees in
ou urc
employees_dup to a new value of 30000. Execute the procedure from an anonymous block, then
SELECT from the table to check that the procedure has executed correctly.
CREATE PROCEDURE pay_raise IS
o
aC s
BEGIN
vi y re
UPDATE employees_dup
SET salary = 30000;
ed d
END pay_raise;
ar stu
BEGIN
is
pay_raise;
Th
END;
SELECT *
sh
FROM employees_dup;
6. Retrieve your first name_change procedure by clicking on its name in the Saved SQL window. Modify
the code to remove OR REPLACE from the CREATE statement, and introduce a deliberate error into
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
This study source was downloaded by 100000814899389 from CourseHero.com on 04-21-2021 22:31:18 GMT -05:00
https://www.coursehero.com/file/41371892/PLSQL-8-1-Practicedocx/
4
the code, for example by misspelling a keyword: UPDAT employees_dup. Execute your code to
recreate the procedure. What happens?
CREATE PROCEDURE pay_raise IS
BEGIN
UPDAT employees_dup
SET salary = 30000;
END pay_raise;
BEGIN
pay_raise;
END;
m
er as
co
SELECT *
eH w
FROM employees_dup;
o.
rs e
ou urc
It raises an error when executing the code.
7. Now correct the procedure code by reinserting the OR REPLACE clause and correcting your
o
deliberate spelling error. Execute your code to recreate the procedure. Now what happens?
aC s
vi y re
8. Create, save, and execute a procedure which updates the salary of employees in
employees_dup according to the following rules:
ed d
ar stu
You will need to include three UPDATE statements, one for each of the above rules. In a later
lesson you will learn how to avoid this. Execute your procedure from an anonymous block and
verify that the updates have been performed correctly.
sh
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
This study source was downloaded by 100000814899389 from CourseHero.com on 04-21-2021 22:31:18 GMT -05:00
https://www.coursehero.com/file/41371892/PLSQL-8-1-Practicedocx/
Powered by TCPDF (www.tcpdf.org)