Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Using Subqueries

FETCH c1 INTO name1; FETCH c1 INTO name2; FETCH c1 INTO name3; CLOSE c1;

END;

/

--this fetches first row

--this fetches second row

--this fetches third row

If you fetch past the last row in the result set, the values of the target variables are undefined.

Note: Eventually, the FETCH statement fails to return a row. When that happens, no exception is raised. To detect the failure, use the cursor attribute %FOUND or %NOTFOUND. For more information, see "Using Cursor Expressions" on page 6-27.

Fetching Bulk Data with a Cursor

The BULK COLLECT clause lets you fetch all rows from the result set at once (see "Retrieving Query Results into Collections with the BULK COLLECT Clause" on page 11-15). In the following example, you bulk-fetch from a cursor into two collections:

DECLARE

TYPE NumTab IS TABLE OF employees.employee_id%TYPE; TYPE NameTab IS TABLE OF employees.last_name%TYPE; nums NumTab;

names NameTab; CURSOR c1 IS

SELECT employee_id, last_name FROM employees

WHERE job_id = 'ST_CLERK';

BEGIN OPEN c1;

FETCH c1 BULK COLLECT INTO nums, names;

--Here is where you iterate through the elements in the NUMS and

--NAMES collections.

NULL; CLOSE c1;

END;

/

Closing a Cursor

The CLOSE statement disables the cursor, and the result set becomes undefined. Once a cursor is closed, you can reopen it, which runs the query again with the latest values of any cursor parameters and variables referenced in the WHERE clause. Any other operation on a closed cursor raises the predefined exception INVALID_CURSOR.

Using Subqueries

A subquery is a query (usually enclosed by parentheses) that appears within another SQL data manipulation statement. The statement acts upon the single value or set of values returned by the subquery. For example:

You can use a subquery to find the MAX(), MIN(), or AVG() value for a column, and use that single value in a comparison in a WHERE clause.

You can use a subquery to find a set of values, and use this values in an IN or NOT IN comparison in a WHERE clause. This technique can avoid joins.

Performing SQL Operations from PL/SQL 6-13

Using Subqueries

You can filter a set of values with a subquery, and apply other operations like ORDER BY and GROUP BY in the outer query.

You can use a subquery in place of a table name, in the FROM clause of a query. This technique lets you join a table with a small set of rows from another table, instead of joining the entire tables.

You can create a table or insert into a table, using a set of rows defined by a subquery.

DECLARE CURSOR c1 IS

-- The main query returns only rows where the salary is greater than the average salary.

SELECT employee_id, last_name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);

CURSOR c2 IS

--The subquery returns all the rows in descending order of salary.

--The main query returns just the top 10 highest-paid employees. SELECT * FROM

(SELECT last_name, salary FROM employees ORDER BY salary DESC, last_name) WHERE ROWNUM < 11;

BEGIN

FOR person IN c1 LOOP

dbms_output.put_line('Above-average salary: ' || person.last_name); END LOOP;

FOR person IN c2 LOOP

dbms_output.put_line('Highest paid: ' || person.last_name || ' $' || person.salary);

END LOOP;

-- The subquery identifies a set of rows to use with CREATE TABLE or INSERT. EXECUTE IMMEDIATE

'CREATE TABLE temp AS (SELECT * FROM employees WHERE salary > 5000)'; EXECUTE IMMEDIATE 'DROP TABLE temp';

END;

/

Using a subquery in the FROM clause, the following query returns the number and name of each department with five or more employees:

DECLARE CURSOR c1 IS

SELECT t1.department_id, department_name, staff FROM departments t1,

(

SELECT department_id, COUNT(*) as staff FROM employees

GROUP BY department_id

) t2 WHERE

t1.department_id = t2.department_id AND staff >= 5;

BEGIN

FOR dept IN c1 LOOP

dbms_output.put_line('Department = ' || dept.department_name || ', staff = ' || dept.staff);

END LOOP;

6-14 PL/SQL User's Guide and Reference

Соседние файлы в папке Oracle 10g