09.01.2015 Views

PL/SQL User's Guide and Reference

PL/SQL User's Guide and Reference

PL/SQL User's Guide and Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using <strong>PL</strong>/<strong>SQL</strong> Collections with <strong>SQL</strong> Statements<br />

IF new_cost IS NOT NULL THEN<br />

my_projects(i).cost := new_cost;<br />

END IF;<br />

EXIT;<br />

END IF;<br />

END LOOP;<br />

UPDATE department SET projects = my_projects<br />

WHERE dept_no = dept_id;<br />

END update_project;<br />

Example: Performing INSERT, UPDATE, <strong>and</strong> DELETE Operations on<br />

<strong>PL</strong>/<strong>SQL</strong> Nested Tables<br />

To perform DML operations on a <strong>PL</strong>/<strong>SQL</strong> nested table, use the operators TABLE<br />

<strong>and</strong> CAST. This way, you can do set operations on nested tables using <strong>SQL</strong> notation,<br />

without actually storing the nested tables in the database.<br />

The oper<strong>and</strong>s of CAST are <strong>PL</strong>/<strong>SQL</strong> collection variable <strong>and</strong> a <strong>SQL</strong> collection type<br />

(created by the CREATE TYPE statement). CAST converts the <strong>PL</strong>/<strong>SQL</strong> collection to<br />

the <strong>SQL</strong> type.<br />

The following example counts the number of differences between a revised course<br />

list <strong>and</strong> the original (notice that the number of credits for course 3720 changed from<br />

4 to 3):<br />

DECLARE<br />

revised CourseList :=<br />

CourseList(Course(1002, ’Expository Writing’, 3),<br />

Course(2020, ’Film <strong>and</strong> Literature’, 4),<br />

Course(2810, ’Discursive Writing’, 4),<br />

Course(3010, ’Modern English Grammar ’, 3),<br />

Course(3550, ’Realism <strong>and</strong> Naturalism’, 4),<br />

Course(3720, ’Introduction to Shakespeare’, 3),<br />

Course(3760, ’Modern Drama’, 4),<br />

Course(3822, ’The Short Story’, 4),<br />

Course(3870, ’The American Novel’, 5),<br />

Course(4210, ’20th-Century Poetry’, 4),<br />

Course(4725, ’Advanced Workshop in Poetry’, 5));<br />

num_changed INTEGER;<br />

BEGIN<br />

SELECT COUNT(*) INTO num_changed<br />

FROM TABLE(CAST(revised AS CourseList)) new,<br />

TABLE(SELECT courses FROM department<br />

<strong>PL</strong>/<strong>SQL</strong> Collections <strong>and</strong> Records 5-25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!