Basic PL/SQL question involving a SELECT statement.

From: Martyn Rankin <mgf_rankin_at_yahoo.com>
Date: 16 May 2003 03:25:33 -0700
Message-ID: <c4147d77.0305160225.48d425a3_at_posting.google.com>



[Quoted] [Quoted] Hi, I am new to PL/SQL and am wondering if it possible in PL/SQL to incorporate two select statements in a script? let me explain why I am asking this question.

For example, I have a database table with a column containing all the divisions in a company and another table storing the financial data. I want to show the budget for each division. I want to incorporate the following SQL statements:

(i) SELECT div FROM divisions;

(ii) SELECT SUM(budget) bd FROM transactions

     WHERE unit like 'x' -- x is the string for the division name

so far, I have declared my cursor and the script prints out all the divisions in the 'divisions' table. So where in my code (below) should I declare (and can the second SELECT statement to represent the SQL in
(ii) - in the DECLARE section or the BEGIN? Am I correct in declaring
[Quoted] the cursor for the divisions? If I create a table view to join the transactions & divisions table, would that make things easier?

This is my code so far:

DECLARE
[Quoted]   CURSOR divisions_cursor IS
    SELECT div FROM divisions;
BEGIN
/* open cursor */
[Quoted] FOR divisions_record IN divisions_cursor LOOP   DBMS_OUTPUT.PUT_LINE ('Division is ' || divisions_record.div);   EXIT WHEN divisions_cursor%NOTFOUND;
  END LOOP;
/* close cursor and end */

I can't find any help on PL/SQL tutorials and I cannot find the answer to my question in my O'Reilly book & would gratefully appreciate any advice or tips.

Thankyou for your time in advance,

Martyn Received on Fri May 16 2003 - 12:25:33 CEST

Original text of this message