Manipulating individual elements - pl/sql, PL-SQL Programming

Assignment Help:

Manipulating Individual Elements

Faraway you have manipulated an entire collection. Within the SQL, to manipulate the individual elements of the collection, and then use the operator TABLE. The operand of TABLE is a subquery which returns a single column value for you to manipulate. That the value is the nested table or the varray.
In the illustration below, you add a row to the History Department nested table stored in the column courses:



BEGIN
INSERT INTO
TABLE(SELECT courses FROM department WHERE name = ’History’)
VALUES(3340, ’Modern China’, 4);
END;


In the next illustration, you revise the number of credits for two courses offered by the Psychology Department:

DECLARE
adjustment INTEGER DEFAULT 1;
BEGIN
UPDATE TABLE(SELECT courses FROM department
WHERE name = ’Psychology’)
SET credits = credits + adjustment
WHERE course_no IN (2200, 3540);
END;

In the following illustration, you retrieve the number and the title of a specific course offered by the History Department:

DECLARE
my_course_no NUMBER(4);
my_title VARCHAR2(35);
BEGIN
SELECT course_no, title INTO my_course_no, my_title
FROM TABLE(SELECT courses FROM department
WHERE name = ’History’)
WHERE course_no = 3105;
...
END;


In the next illustration, you delete all 5-credit courses offered by the English Department:

BEGIN
DELETE TABLE(SELECT courses FROM department
WHERE name = ’English’)
WHERE credits = 5;
END;



In the following illustration, you recover the title and cost of the Maintenance Department’s fourth project from the varray column projects:

DECLARE
my_cost NUMBER(7,2);
my_title VARCHAR2(35);
BEGIN
SELECT cost, title INTO my_cost, my_title
FROM TABLE(SELECT projects FROM department
WHERE dept_id = 50)
WHERE project_no = 4;
...
END;


Presently, you cannot reference the individual elements of a varray in an UPDATE, INSERT, or DELETE statement. And hence, you should use the PL/SQL procedural statements. In the illustration below, the stored procedure add_project inserts a new project into the department’s project list at a given position a shown:

CREATE PROCEDURE add_project (
dept_no IN NUMBER,
new_project IN Project,
position IN NUMBER) AS
my_projects ProjectList;
BEGIN

SELECT projects INTO my_projects FROM department
WHERE dept_no = dept_id FOR UPDATE OF projects;
my_projects.EXTEND; -- make room for new project
/* Move varray elements forward. */
FOR i IN REVERSE position..my_projects.LAST - 1 LOOP
my_projects(i + 1) := my_projects(i);
END LOOP;
my_projects(position) := new_project; -- add new project
UPDATE department SET projects = my_projects
WHERE dept_no = dept_id;
END add_project;

The stored procedure updates below for a given project is:

CREATE PROCEDURE update_project (
dept_no IN NUMBER,
proj_no IN NUMBER,
new_title IN VARCHAR2 DEFAULT NULL,
new_cost IN NUMBER DEFAULT NULL) AS
my_projects ProjectList;
BEGIN
SELECT projects INTO my_projects FROM department
WHERE dept_no = dept_id FOR UPDATE OF projects;
/* Find project, update it, then exit loop immediately. */
FOR i IN my_projects.FIRST..my_projects.LAST LOOP
IF my_projects(i).project_no = proj_no THEN
IF new_title IS NOT NULL THEN
my_projects(i).title := new_title;
END IF;
IF new_cost IS NOT NULL THEN
my_projects(i).cost := new_cost;
END IF;
EXIT;
END IF;
END LOOP;
UPDATE department SET projects = my_projects
WHERE dept_no = dept_id;
END update_project;


Related Discussions:- Manipulating individual elements - pl/sql

Triggers, At times, customers make mistakes in submitting their orders and ...

At times, customers make mistakes in submitting their orders and call to cancel the order. Brewbean’s wants to create a trigger that automatically updates the stock level of all pr

Theory of special creation - origin of life, THEO R Y OF SPECIAL CREATION...

THEO R Y OF SPECIAL CREATION - Life originated on the earth due to natural events by the super natural power. The biblical story of creation of world within six days was p

Mysql developer/programmer, Project Description: I want somebody who can...

Project Description: I want somebody who can help me with an idea that I have been working on for a few months now. The person will require extensive knowledge of warcraft 3 PvP

Sql functions, SQL Functions The PL/SQL uses all the SQL functions invo...

SQL Functions The PL/SQL uses all the SQL functions involving the following aggregate functions that summarize the whole columns of the Oracle data: GROUPING, AVG, COUNT, STDDE

Collection methods in pl sql, Collection Methods:   The collection me...

Collection Methods:   The collection method is a built-in function or procedure which operates on the collections and is called using the dot notation. The methods like the C

Parameter and keyword description - records, Parameter and Keyword Descript...

Parameter and Keyword Description: record_type_name: This identifies the user-defined type specifier that is used in the subsequent declarations of the records. NOT N

Ensuring backward compatibility, Ensuring Backward Compatibility   The...

Ensuring Backward Compatibility   The PL/SQL Version 2 permits some abnormal behavior which Version 8 disallows. Particularly, Version 2 permits you to (i) Make the forw

Write an anonymous block that contains a pl/sql function, a. Write an anon...

a. Write an anonymous block that contains a PL/SQL function. Given an order number orderNo, the function will calculate the total number of the parts in the order. Then the anonym

Theory of spontaneous generation - origin of life, THEORY OF SPONTANEOUS GE...

THEORY OF SPONTANEOUS GENERATION - ABIOGENESIS OR AUTOGENESIS - According to this theory, the existing living communities have originated from non-living organic matter with

Boolean values-assignments in pl/sql, Boolean Values Only the values TRU...

Boolean Values Only the values TRUE, FALSE, & NULL can be assigned to a Boolean variable. For illustration, given the declaration DECLARE done BOOLEAN; the following statements

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd