0

I'm very new to SQL and would like to know how to get some data from a table and place it into an array. If I have this:

SELECT SeatNo FROM SEATING_PLAN
WHERE Block = 1;

SeatNo and Block are INT.

What sort of array should I use to place all of the seat numbers that match into the array, and what sort of loop is best for this?

Thanks in advance

3
  • 1
    Why do you need to place it in an array? An array within the database or in another language? There should be no need to loop... Commented Oct 27, 2013 at 11:09
  • There is a need to loop if you are using say JDBC to access the database from an external Java application. Which programming language, and are you writing an external application, or embedded code in a stored procedure? Commented Oct 27, 2013 at 11:11
  • It is for an assignment. I have already handed it in, but I would like to improve it. The procedure checks how many seats in a certain block, then counts how many have been booked. if there are seats left then it continues with a booking. I would like to improve it by checking what seat numbers are taken and then assigning spare seats. I just wanted to put all of the seats into an array and then check against the book ones, as soon as a seat that wasn't booked was compared the booking would continue. Commented Oct 27, 2013 at 12:04

1 Answer 1

1

In PL/SQL you have 2 types of arrays, associative arrays and variant arrays. There is another type of collection in PL/SQL called Nested Tables. This profussion makes PL/SQL obscure as the three are treated similarly for some operations and completely different for other.

http://docs.oracle.com/cd/B12037_01/appdev.101/b10807/05_colls.htm

If you want to store scalar type collections, I recommend you to use associative arrays:

DECLARE
   TYPE t_SeatNo is TABLE OF SEATING_PLAN.SeatNo%TYPE INDEX BY BINARY_INTEGER;
   seats t_SeatNo;
BEGIN
   SELECT SeatNo
   BULK COLLECT INTO seats
   FROM SEATING_PLAN
   WHERE Block = 1;

   -- Do what you need with the data

END;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.