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

Using default-declarations in sql, Using DEFAULT You can use the keyword...

Using DEFAULT You can use the keyword DEFAULT rather than that of the assignment operator to initialize the variables. For e.g. the declaration blood_type CHAR := ’O’; it can b

Max and min operator in sql, MAX and MIN operator in SQL Example: ...

MAX and MIN operator in SQL Example: (SELECT MAX (Mark) FROM EXAM_MARK WHERE StudentId = 'S1') (SELECT MIN (Mark) FROM EXAM_MARK WHERE StudentId = 'S1') Example

Close statement in pl sql, CLOSE Statement The CLOSE statement allows ...

CLOSE Statement The CLOSE statement allows the resources held by a cursor variable or open cursor to be reused. No more rows can be fetched from the cursor variable or closed

Wrapping and unwrapping in sql, Wrapping and unwrapping in SQL Operato...

Wrapping and unwrapping in SQL Operators WRAP and UNWRAP in connection with attributes whose declared types are tuple types. Example shows how extension and projection can be

Types of evolution, TYPES OF EVOLUTION - Sequential evolution         ...

TYPES OF EVOLUTION - Sequential evolution                  :                    Minor changes in the gene pool of a population from one generation to the next, with the resul

Initializing objects in pl sql, Initializing Objects: Till you initiali...

Initializing Objects: Till you initialize an object by calling the constructor for its object type, the object is automatically null. That is, the object itself is null, not me

Database values-assignments in pl/sql, Database Values You can use the S...

Database Values You can use the SELECT statement to have the Oracle assign values to a variable. For Each and every item in the select list, there must be a matching, type-compa

Keyword & parameter description - exceptions, Keyword & Parameter Descripti...

Keyword & Parameter Description: WHEN: This keyword introduces the exception handler. You can have many exceptions execute the similar sequence of the statements by follo

Effects of null operator, Effects of NULL Operator As a general rule-b...

Effects of NULL Operator As a general rule-but not a universal one-if NULL is an argument to an invocation of a system-defined read-only operator, then NULL is the result of t

Declaring exceptions - user-defined exceptions, Declaring Exceptions T...

Declaring Exceptions The Exceptions can be declared only in the declarative part of the PL/SQL subprogram, block, or package. By introducing its name, you can declare an excep

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