Initializing varray's
DBAsupport.com Forums - Powered by vBulletin
Results 1 to 2 of 2

Thread: Initializing varray's

  1. #1
    Join Date
    Feb 2001
    Posts
    6
    I have multiple open/close times for one day, and I want to keep a week of them (this is a PL/SQL only problem, the data isn't going to the database). I did this by creating a record for the day, then a varray for the week, e.g.

    TYPE day_sched is RECORD (
    open1 int,
    close1 int,
    open2 int,
    close2 int,
    ...
    close6 int);
    TYPE week_sched is VARRAY(7) OF day_sched;
    i_week_sched week_sched;

    My question is: is it possible to initialize i_week_sched with one command, or do I have to do a loop, or what? The only thing I've been able to get to work is to define another variable that is a day_sched, initialize each of the variables in that record, then initialize the weekly schedule, i.e.
    i_day_sched day_sched;
    i_day_sched.open1 := 0;
    i_day_sched.close1 := 0;
    ...
    i_week_sched := week_sched(i_day_sched, i_day_sched...);

    I feel sure there's an easier way to do this, but I haven't been able to find it from the PL/SQL manual or from the Complete Reference.

    Thanks,

    Vince

  2. #2
    Join Date
    Apr 2001
    Posts
    17

    Cool

    Hi Vince,

    I had problems finding docco on this as well. Here's what you're looking for:

    DECLARE
    TYPE day_sched IS RECORD(open1 PLS_INTEGER, close1 PLS_INTEGER,
    open2 PLS_INTEGER, close2 PLS_INTEGER);
    TYPE week_sched IS VARRAY(7) OF day_sched;

    i_week_sched week_sched;
    BEGIN
    -- Initialise
    i_week_sched := week_sched();
    -- Add an element
    i_week_sched.EXTEND;
    -- Insert an element
    i_week_sched(i_week_sched.LAST).open1 := 1;
    -- Add an element
    i_week_sched.EXTEND;
    -- Insert an element
    i_week_sched(i_week_sched.LAST).open1 := 2;
    -- Print the elements
    FOR i IN i_week_sched.FIRST..i_week_sched.LAST
    LOOP
    DBMS_OUTPUT.PUT_LINE(i_week_sched(i).open1);
    END LOOP;
    END;

    Cheers,
    CM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width