Skip to content

Commit

Permalink
More PLSQL scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Brumm committed Jun 26, 2024
1 parent af9fc43 commit fa7a858
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 0 deletions.
36 changes: 36 additions & 0 deletions courses/course_effective_plsql/18_multiple_input.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* 01 */

CREATE OR REPLACE PROCEDURE ShowEmployeesHiredInRange(
v_startdate IN DATE,
v_enddate IN DATE
)
AS
v_empid NUMBER;
v_firstname VARCHAR2(100);
v_lastname VARCHAR2(100);
v_hiredate DATE;
BEGIN
SELECT employee_id, first_name, last_name, hire_date
INTO v_empid, v_firstname, v_lastname, v_hiredate
FROM employees
WHERE hire_date BETWEEN v_startdate AND v_enddate
FETCH FIRST 1 ROWS ONLY;

DBMS_OUTPUT.PUT_LINE('Employee ' || v_empid || ' ' ||
v_firstname || ' ' || v_lastname || ' was hired on ' ||
v_hiredate);
END;

CALL ShowEmployeesHiredInRange(DATE'2005-01-01', DATE'2006-12-13');

CALL ShowEmployeesHiredInRange(DATE'2006-12-13', DATE'2005-01-01');

CALL ShowEmployeesHiredInRange(
v_startdate => DATE'2005-01-01',
v_enddate => DATE'2006-12-13'
);

CALL ShowEmployeesHiredInRange(
v_enddate => DATE'2006-12-13',
v_startdate => DATE'2005-01-01'
);
32 changes: 32 additions & 0 deletions courses/course_effective_plsql/19_insert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
SELECT *
FROM employees;


CREATE OR REPLACE PROCEDURE InsertEmployeeSimple(
v_firstname IN VARCHAR2,
v_lastname IN VARCHAR2,
v_email IN VARCHAR2,
v_salary IN NUMBER
)
AS
BEGIN
INSERT INTO employees (employee_id, first_name, last_name, email,
phone_number, hire_date, job_id, salary, commission_pct,
manager_id, department_id)
VALUES (employees_seq.nextval, v_firstname, v_lastname, v_email,
NULL, SYSDATE, 'SH_CLERK', v_salary, NULL,
NULL, 50);

END;

CALL InsertEmployeeSimple(
v_firstname => 'Ben',
v_lastname => 'Brumm',
v_email => 'BBRUMM',
v_salary => 1000
);


SELECT *
FROM employees
ORDER BY employee_id DESC;
25 changes: 25 additions & 0 deletions courses/course_effective_plsql/20_update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SELECT *
FROM employees;

CREATE OR REPLACE PROCEDURE UpdatePeopleContactDetails(
v_emp_id NUMBER,
v_phonenumber VARCHAR2,
v_emailaddress VARCHAR2
)
AS
BEGIN
UPDATE employees
SET phone_number = v_phonenumber,
email = v_emailaddress
WHERE employee_id = v_emp_id;
END;

CALL UpdatePeopleContactDetails(
v_emp_id => 207,
v_phonenumber => '515.123.1111',
v_emailaddress => 'BENBRUMM'
);

SELECT *
FROM employees
WHERE employee_id = 207;
12 changes: 12 additions & 0 deletions courses/course_effective_plsql/21_delete.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE OR REPLACE PROCEDURE DeleteEmployee(
v_empid IN NUMBER
)
AS
BEGIN
DELETE FROM employees
WHERE employee_id = v_empid;

DBMS_OUTPUT.PUT_LINE('Employee ID ' || v_empid || ' deleted.');
END;

CALL DeleteEmployee(207);
8 changes: 8 additions & 0 deletions courses/course_effective_plsql/22_call.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CALL FindEmployeeName(102);

DECLARE
found_first_name VARCHAR2(100);
BEGIN
FindEmployeeName(101, found_first_name);
DBMS_OUTPUT.PUT_LINE(found_first_name);
END;
13 changes: 13 additions & 0 deletions courses/course_effective_plsql/22_out_params.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE OR REPLACE PROCEDURE FindEmployeeName(
v_employee_id IN NUMBER,
v_first_name OUT VARCHAR
)
AS
BEGIN
SELECT first_name
INTO v_first_name
FROM employees
WHERE employee_id = v_employee_id;

END;

68 changes: 68 additions & 0 deletions courses/course_effective_plsql/29_flow.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
CREATE OR REPLACE PROCEDURE ShowEmployeesHiredInRange(
v_startdate IN DATE,
v_enddate IN DATE
)
AS
v_empid NUMBER;
v_firstname VARCHAR2(100);
v_lastname VARCHAR2(100);
v_hiredate DATE;
BEGIN
SELECT employee_id, first_name, last_name, hire_date
INTO v_empid, v_firstname, v_lastname, v_hiredate
FROM employees
WHERE hire_date BETWEEN v_startdate AND v_enddate
FETCH FIRST 1 ROWS ONLY;

DBMS_OUTPUT.PUT_LINE('Employee ' || v_empid || ' ' ||
v_firstname || ' ' || v_lastname || ' was hired on ' ||
v_hiredate);
END;


CALL ShowEmployeesHiredInRange(
v_startdate => DATE'2005-01-01',
v_enddate => DATE'2006-12-13'
);


/*
Add IF
*/

CREATE OR REPLACE PROCEDURE ShowEmployeesHiredInRange(
v_startdate IN DATE,
v_enddate IN DATE
)
AS
v_empid NUMBER;
v_firstname VARCHAR2(100);
v_lastname VARCHAR2(100);
v_hiredate DATE;
BEGIN
IF v_startdate > v_enddate THEN
DBMS_OUTPUT.PUT_LINE('Invalid entry: the start date is after the end date.');
ELSE
SELECT employee_id, first_name, last_name, hire_date
INTO v_empid, v_firstname, v_lastname, v_hiredate
FROM employees
WHERE hire_date BETWEEN v_startdate AND v_enddate
FETCH FIRST 1 ROWS ONLY;

DBMS_OUTPUT.PUT_LINE('Employee ' || v_empid || ' ' ||
v_firstname || ' ' || v_lastname || ' was hired on ' ||
v_hiredate);
END IF;
END;


CALL ShowEmployeesHiredInRange(
v_startdate => DATE'2005-01-01',
v_enddate => DATE'2006-12-13'
);


CALL ShowEmployeesHiredInRange(
v_startdate => DATE'2008-01-01',
v_enddate => DATE'2006-12-13'
);
16 changes: 16 additions & 0 deletions courses/course_effective_plsql/30_basic_loop.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE OR REPLACE PROCEDURE ListHelloBasic
AS
v_loop_counter NUMBER;
BEGIN
v_loop_counter := 1;
LOOP
DBMS_OUTPUT.PUT_LINE('Hello');
v_loop_counter := v_loop_counter + 1;

IF (v_loop_counter > 10) THEN
EXIT;
END IF;
END LOOP;
END;

CALL ListHelloBasic();
20 changes: 20 additions & 0 deletions courses/course_effective_plsql/30_insert_locations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
SELECT *
FROM locations;

CREATE OR REPLACE PROCEDURE InsertLocations
AS
v_loop_counter NUMBER;
BEGIN
LOOP
INSERT INTO locations (location_id, street_address, postal_code,
city, state_province, country_id)
VALUES (locations_seq.nextval, 'Temp Address', 12345,
'Sydney', 'New South Wales', 'AU');

v_loop_counter := v_loop_counter + 1;

IF (v_loop_counter > 4) THEN
EXIT;
END IF;
END LOOP;
END;

0 comments on commit fa7a858

Please sign in to comment.