Vous êtes sur la page 1sur 9

www.oracle.

com/academy

Database Programming with PL/SQL


8-1: Creating Procedures Practice
Activities
Vocabulary
Identify the vocabulary word for each definition below:

Pl/SQL subprograms Named PL/SQL blocks that are compiled and stored in the
database.

Identifier Indicates the DECLARE section of a subprogram.

Anonymous Block Unnamed executable PL/SQL blocks that cannot be reused or


stored in the database for later use.

Procedures Named PL/SQL blocks that can accept parameters and are
compiled and stored in the database.

Try It / Solve It
1. What is the difference between the following two pieces of code?

CODE SAMPLE A

DECLARE
v_empid employees.employee_id%TYPE := 100;
v_percent_increase NUMBER(2,2) := .05;
BEGIN
UPDATE employees
SET salary = (salary * v_percent_increase) + salary
WHERE employee_id = v_empid;
END;
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.
2

CODE SAMPLE B

CREATE PROCEDURE pay_raise


(p_empid employees employee_id%TYPE,
p_percent_increase NUMBER)
IS
BEGIN
UPDATE employees
SET salary = (salary * p_percent_increase) + salary
WHERE employee_id = p_empid;
END pay_raise;

Ans. The difference is that code sample B is a procedure and can be reused in other
database statements making the same update easy for someone else who is tasked
with updating the database with the same values. While Sample A is only a one time
use Update statement. If someone wants to update the database again then they with
either have to create a pprocedure to handle the update or recreate the code in
sample A. Sample Code B is always preferred.

2. In your own words, list the benefits of subprograms.

Ans. A subprogram allows you to reuse sections of code without having to retype it all. It
also lets you design pl sql code to your needs, for example, you can create a subprogram
to create new departments in a business and just call it with a few lines of code not having
to design the whole department from scratch.

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.
3

3. In your own words, describe a stored procedure.


Ans. A stored procedure is a set of sql statements that are given a set name, these sql statements
can then be called from other programs to midify data in any databse. Stored procedures are not tied
to any specific object or database.

4. The remaining questions in this practice use a copy of the employees table. Create the copy by
executing the following SQL statement:

CREATE TABLE employees_dup AS SELECT * from employees;

Table created.

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 name change procedure.”

CREATE OR REPLACE PROCEDURE name_change IS


BEGIN
UPDATE employees_dup
SET first_name = 'Susan'
WHERE department_id = 80;
END name_change;

Procedure created.

0.03 seconds

B. Execute the procedure by running the following anonymous block:


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.
4

BEGIN
name_change;
END;

Statement processed.

0.01 seconds

C. SELECT from the table to check that the procedure has executed correctly and performed the
UPDATE.

Select * from employees_dup where department_id = 80;


EMPLOYEE_ FIRST_ LAST_ HIRE_ JOB_ COMMISSION_ MANAGER_ D
EMAIL PHONE_NUMBER SALARY
ID NAME NAME DATE ID PCT ID
29-
SA_
149 Susan Zlotkey EZLOTKEY011.44.1344.429018 Jan- 10500 .2 100 8
MAN
2015
11-
SA_R
174 Susan Abel EABEL 011.44.1644.429267 May- 11000 .3 149 8
EP
2011
24-
SA_R
176 Susan Taylor JTAYLOR 011.44.1644.429265 Mar- 8600 .2 149 8
EP
2013
01- SR_S
212 Susan HooperNHOOPER 011.44.1886 6663 Sep- A_RE 9600 .2 149 8
2012 P

5. Create a second procedure named pay_raise which changes the salary of all employees in
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 or replace procedure pay_raise is


Begin
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.
5

Update employees_dup
Set salary = '30000';
End pay_raise;

Procedure created.

0.02 seconds

begin
pay_raise;
end;

Statement processed.

0.01 seconds

select First_name, last_name, Salary from employees_dup

FIRST_NAME LAST_NAME SALARY


Steven King 30000
Neena Kochhar 30000
Lex De Haan 30000
Jennifer Whalen 30000
Shelley Higgins 30000
William Gietz 30000
Susan Zlotkey 30000
Susan Abel 30000
Susan Taylor 30000
Kimberely Grant 30000
Kevin Mourgos 30000
Trenna Rajs 30000
Curtis Davies 30000

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.
6

Randall Matos 30000


Peter Vargas 30000
Alexander Hunold 30000
Bruce Ernst 30000
Diana Lorentz 30000
Michael Hartstein 30000
Pat Fay 30000
Sophia Barbosa Souza 30000
Diego Silva Pinto 30000
Sarah Alves Rocha 30000
Lucas Almeida Castro 30000
Susan Hooper 30000
Donna Steiner 30000
George Bell 30000
Lisa TAYLOR 30000
Michael Stocks 30000
Tiffany Heiden 30000
Chen Li 30000
Alain Fontaine 30000
Matthias Reinhard 30000
Katia Hernandez 30000
Guido Ricci 30000
Mizuto Saikawa 30000
Nabil Safwah 30000
Jelena Duric 30000
Jennifer Loermans 30000
Alice Newton 30000

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.
7

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 the code, for example by misspelling a keyword: UPDAT employees_dup. Execute your
code to recreate the procedure. What happens?
Ans. Error message saying name is already being used by an existing object.

7. Now correct the procedure code by reinserting the OR REPLACE clause and correcting your
deliberate spelling error. Execute your code to recreate the procedure. Now what happens?
CREATE OR REPLACE PROCEDURE name_change IS
BEGIN
UPDATE employees_dup
SET first_name = 'Susan'
WHERE department_id = 80;
END name_change;

Procedure created.

0.01 seconds

Ans. Code executed successfully.

8. Create, save, and execute a procedure which updates the salary of employees in
employees_dup according to the following rules:

- if the employee is in department 80, the new salary = 1000


- if the employee is in department 50, the new salary = 2000
- if the employee is in any other department, the new salary = 3000.

Create or replace procedure new_sal is


Begin
Update employees_dup
Set salary = 1000
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.
8

where department_id = 80;


Update employees_dup
set salary = 2000
where department_id = 50;
Update employees_dup
set salary = 3000
where department_id NOT IN (80,50);
end new_sal;

Procedure created.

Select department_id, salary from employees_dup;


DEPARTMENT_ID SALARY
90 3000
90 3000
90 3000
10 3000
110 3000
110 3000
80 1000
80 1000
80 1000
- 7000
50 2000
50 2000
50 2000
50 2000
50 2000
60 3000
60 3000
60 3000

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.
9

20 3000
20 3000
85 3000
85 3000
85 3000
85 3000
80 1000
20 3000
50 2000
20 3000
20 3000
50 2000
60 3000
60 3000
110 3000
10 3000
10 3000
10 3000
20 3000
110 3000
110 3000
20 3000

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.

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.

Vous aimerez peut-être aussi