0% found this document useful (0 votes)
10 views2 pages

Pu 01 Creating Stored Procedures Scripts

The document provides examples of creating stored procedures in SQL, including procedures for updating employee salaries, querying employee details, formatting phone numbers, adding departments, and logging employee deletions. Each example demonstrates the syntax and usage of parameters, as well as how to execute and print results. The procedures utilize various SQL features such as variable declaration and default parameter values.

Uploaded by

elsaidageeb86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Pu 01 Creating Stored Procedures Scripts

The document provides examples of creating stored procedures in SQL, including procedures for updating employee salaries, querying employee details, formatting phone numbers, adding departments, and logging employee deletions. Each example demonstrates the syntax and usage of parameters, as well as how to execute and print results. The procedures utilize various SQL features such as variable declaration and default parameter values.

Uploaded by

elsaidageeb86
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

PU Chapter 01 Creating Stored Procedures Scripts

Example (pu_01_01)

CREATE OR REPLACE PROCEDURE pu_01_01


(p_id IN employees.employee_id%TYPE)
IS
BEGIN
UPDATE employees
SET salary = salary * 1.10
WHERE employee_id = p_id;
END pu_01_01;
/
--------------------------------------------------------------------------------------
Example (pu_01_02)

CREATE OR REPLACE PROCEDURE query_emp


(p_id IN employees.employee_id%TYPE,
p_name OUT employees.last_name%TYPE,
p_salary OUT employees.salary%TYPE,
p_comm OUT employees.commission_pct%TYPE)
IS
BEGIN
SELECT last_name, salary, commission_pct
INTO p_name, p_salary, p_comm
FROM employees
WHERE employee_id = p_id;
END query_emp;
/

VARIABLE g_name VARCHAR2(25)


VARIABLE g_sal NUMBER
VARIABLE g_comm NUMBER
EXECUTE query_emp(103, :g_name, :g_sal, :g_comm)
PRINT g_name
--------------------------------------------------------------------------------------
Example (pu_01_03)

CREATE OR REPLACE PROCEDURE format_phone


(p_phone_no IN OUT VARCHAR2)
IS
BEGIN
p_phone_no := '(' || SUBSTR(p_phone_no,1,3) ||
')' || SUBSTR(p_phone_no,4,3) ||
'-' || SUBSTR(p_phone_no,7);
END format_phone;
/

VARIABLE g_phone_no VARCHAR2(15)


BEGIN
:g_phone_no := '8006330575';
END;
/
PRINT g_phone_no
EXECUTE format_phone (:g_phone_no)
PRINT g_phone_no
--------------------------------------------------------------------------------------
Example (pu_01_04)

CREATE OR REPLACE PROCEDURE add_dept


(p_name IN departments.department_name%TYPE
DEFAULT 'unknown',
p_loc IN departments.location_id%TYPE
DEFAULT 1700)
IS
BEGIN
INSERT INTO departments(department_id,
department_name, location_id)
VALUES (departments_seq.NEXTVAL, p_name, p_loc);
END add_dept;
/

BEGIN
add_dept;
add_dept ('TRAINING', 2500);
add_dept ( p_loc => 2400, p_name =>'EDUCATION');
add_dept ( p_loc => 1200) ;
END;
/
--------------------------------------------------------------------------------------
Example (pu_01_05)

CREATE TABLE log_table (user_id VARCHAR2(30), log_date DATE);

CREATE OR REPLACE PROCEDURE leave_emp


(p_id IN employees.employee_id%TYPE)
IS
PROCEDURE log_exec
IS
BEGIN
INSERT INTO log_table (user_id, log_date)
VALUES (USER, SYSDATE);
END log_exec;
BEGIN
DELETE FROM employees
WHERE employee_id = p_id;
log_exec;
END leave_emp;
/
--------------------------------------------------------------------------------------

You might also like