How to divide a collection of PL/SQL in two?

Hello

I try to divide a pl/sql table into two, but ended up with some "nasty" loops

I was wondering if there was an easier way?
Session altered.
SQL> create or replace package my_package
as
   function my_func(required_number_of_elements in number) 
     return sys.odcinumberlist;
end my_package;
Package created.
SQL> create or replace package body my_package
as
      
   function my_func(required_number_of_elements in number) 
     return sys.odcinumberlist
   is
      return_list sys.odcinumberlist;
      log_list    sys.odcinumberlist := sys.odcinumberlist();
      
      procedure split_it
      is
-- Should split list, so 
--   log_list holds return_list(required_number_of_elements + 1) .. return_list(return_list.count) 
--   return_list holds return_list(1) .. return_list(required_number_of_elements)           
      begin
         IF required_number_of_elements < return_list.count
         THEN
            log_list.extend(return_list.count - required_number_of_elements);

            FOR i IN  required_number_of_elements + 1 .. return_list.count
            LOOP
              log_list(i - required_number_of_elements) := return_list(i); 
            END LOOP;
            
            return_list.trim(return_list.count - required_number_of_elements);
            
         END IF;
      end split_it;
   begin

      select object_id
      bulk collect into return_list
      from all_objects
      where rownum <= 5; -- Disregard, just to simplify example
      
      split_it; -- How to write this?

      dbms_output.put_line('return_list.count: ' || return_list.count);
      dbms_output.put_line('log_list.count: ' || log_list.count);            
      return return_list;
   end my_func;
end my_package;
Package body created.
SQL> declare 
  dummy sys.odcinumberlist;
begin 
  dummy := my_package.my_func(3);
  dummy := my_package.my_func(10);
end;
return_list.count: 3
log_list.count: 2
return_list.count: 5
log_list.count: 0
PL/SQL procedure successfully completed.
BANNER                                                          
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
1 row selected.
Concerning
Peter

Published by: Peter on May 13, 2009 02:01
Banner added

How to divide a collection of PL/SQL in two?

Just copy and DELETE the part of the original Collection:

SQL> declare
   c1 sys.dbms_debug_vc2coll;
   c2 sys.dbms_debug_vc2coll;
begin
   select to_char (level)
   bulk collect
   into c1
   from dual
   connect by level <= 10;

   c2 := c1;
   c1.delete (1, 5);
   c2.delete (6, 10);

   for i in c1.first .. c1.last
   loop
      dbms_output.put_line ('C1: ' || c1 (i));
   end loop;

   for i in c2.first .. c2.last
   loop
      dbms_output.put_line ('C2: ' || c2 (i));
   end loop;
end;
/
C1: 6
C1: 7
C1: 8
C1: 9
C1: 10
C2: 1
C2: 2
C2: 3
C2: 4
C2: 5
PL/SQL procedure successfully completed.

Tags: Database

Similar Questions

  • How to divide different my speakers and headphones to two playback devices

    I recently bought the headset and when I plugged yesterday, they worked exactly as I wanted. Sound kept coming through the speakers only and I have only the voice through my headset (voice of my chat software).

    Turn on my computer this morning, however, speakers and headphones were grouped in one reading "Speakers/headphones" and plug the headset would replace the speakers and play all the sound in the headphones. The helmet is always displayed as a separate device, 'Helmet of Communication', and test it still produces sounds in the headphones.
    I would like to know how to divide the headphones and speakers, so they work as two devices separated again.

    It depends on your sound manufacturer and if they have channels separated, programmed for the lines of the map.

  • How to use Bulk collect in dynamic SQL with the example below:

    My Question is

    Using of dynamic SQL with collection in bulkif we pass the name of the table as "to the parameter' function, I want to display those

    An array of column names without vowels (replace the vowels by spaces or remove vowels and display).

    Please explain for example.

    Thank you!!

    It's just a predefined type

    SQL> desc sys.OdciVarchar2List
     sys.OdciVarchar2List VARRAY(32767) OF VARCHAR2(4000)
    

    You can just as easily declare your own collection type (and you are probably better served declaring your own type of readability if nothing else)

    SQL> ed
    Wrote file afiedt.buf
    
      1  CREATE OR REPLACE
      2     PROCEDURE TBL_COLS_NO_VOWELS(
      3                                  p_owner VARCHAR2,
      4                                  p_tbl   VARCHAR2
      5                                 )
      6  IS
      7     TYPE vc2_tbl IS TABLE OF varchar2(4000);
      8     v_col_list vc2_tbl ;
      9  BEGIN
     10      EXECUTE IMMEDIATE 'SELECT COLUMN_NAME FROM DBA_TAB_COLUMNS WHERE OWNER = :1 AND TABLE_NAME = :2 ORDER BY COLUMN_ID'
     11         BULK COLLECT
     12         INTO v_col_list
     13        USING p_owner,
     14              p_tbl;
     15      FOR v_i IN 1..v_col_list.COUNT LOOP
     16        DBMS_OUTPUT.PUT_LINE(TRANSLATE(v_col_list(v_i),'1AEIOU','1'));
     17      END LOOP;
     18*  END;
    SQL> /
    
    Procedure created.
    
    SQL> exec tbl_cols_no_vowels( 'SCOTT', 'EMP' );
    MPN
    NM
    JB
    MGR
    HRDT
    SL
    CMM
    DPTN
    
    PL/SQL procedure successfully completed.
    

    Justin

  • How perfectly divide a text in a column into two parts, top and bottom?

    Have a long text always with two pieces of text per page, both with paragraphs returns and the need to place one hand upwards and the other downwards, divided in the Center only by the logical space caused by the split. The option to 'justify' block is not useful if the leader is a condition.

    A solution is the treatment the text of "background" as a reference... but it is very rude...

    Screen Shot 2011-08-02 at 2.24.04 PM.png

    Have you looked at the screenshot I posted?

    Even in the fully threaded text, this should work if the Dungeon options are set so that alternating paragraphs are set to start in the next frame or column. You can do this by making two styles, one based on the other, changing only the location of paragraph start between them and by assigning then they are next to each other style then they alternate.

  • How to compare the source of PL/SQL in two databases using a clever trick

    Hello
    Here is my scenario:

    We have a very large (> 50) team of developers who work on PL/SQL.
    As and as new improvements/fixes are tested successfully, DBA migrates the new source code for DEV to QA, then the PROD instance.

    Very often, the developers are invited to check if the source code migrated to the instance target is the right. This Yes, because we use an automated migration tools... .and sometimes DBA can make mistake in migrating the wrong version (say a beta code).

    So that developers are left with comparing the source manually or by using the file compare tools... a very tedious job.
    (I know not a strict discipline to keep a barrette and version will help you; but we have a lot of existing code that does not adhere to this rule)

    So, is it possible to do ' intelligently '... I thought of a way to get the code of HASH/HASHKEY of all lines of source code of a procedure or a pkg and then again to get the HASHCODE/HASHKEY of all codes/HASHKEYs of lines of source for a particular package. to get a unique HASHKEY for a pkg... .and then I can compare this HASHKEY of 2 bodies... and they are same, means the code is the same... Otherwise, the code is different

    for example
    Select custom_hashkey_prc (text) from dba_source, whose name = "MY_PKG.

    Thank you.

    Published by: DT1977 on January 19, 2009 15:31

    Using a HASH to check if the two sources are the same would be a valid way to do a quick check, although it will obviously not be to tell you where the differences are.

    In Oracle 10 g, you can use the DBMS_CRYPTO package to generate HASH values...

    http://www.psoug.org/reference/dbms_crypto.html

    We use this method to detect changes in the data where exist CLOB columns (because we cannot use set operators of SQL on CLOB).

  • How to divide a single line (all lines) for the use of a few sql

    I have table

    Select * from une_table;

    name_a
    date_a
    name_b

    Date_B

    Sunday01.01.2015Monday02.01.2015
    Tuesday03.01.2015Wednesday04.01.2015

    How to divide this table in all lines after using sql:

    name
    Date
    Sunday01.01.2015
    Monday02.01.2015
    Tuesday03.01.2015
    Wednesday04.01.2015

    Thanks for the replies.

    Trajon,

    You don't have a table. You have a store of garbage. There is a repeating group in your 'table' and column name_a etc is unnecessary, because you can derive the day of the week from your date by a to_char function.

    You can find it in your documentation online.

    Knowing that you can get the ordinal number of a date using to_char, what have you tried to do it yourself?

    Your answer is probably: NOTHING.

    First try to solve it yourself and if you post something here, make sure that this forum is not to throw up, because your code that is terribly bad.

    ------------

    Sybrand Bakker

    Senior Oracle DBA

  • How to divide the resultset SQL based on the data in the form of scenarios?

    Hello

    I need to develop a query that should be divided into scenarios

    Ex:

    Scenario 1: If a product is sold to the United States and charged in the United Kingdom
    Scenario 2: If a product is sold to the United Kingdom and charged in the United States
    Scenario 3: If a product is sold and invoiced to the United States
    Scenario 4: If a product is sold and invoiced to the United Kingdom

    Based on 1 how to divide and provide SQL result to users based on the scenarios?

    Ex: Result should show all the data, saying that the result set is in scenario 1, etc..,.

    Please advice

    Thank you
    user12048986

    Hello

    You can use an expression BOX to classify each line:

    CASE  sold_country || ' ' || billed_country
         WHEN  'US UK'     THEN  1
         WHEN  'UK US'     THEN  2
         WHEN  'US US'     THEN  3
         WHEN  'UK UK'     THEN  4
    END     AS scenario_num
    

    If sold_country or billed_country is anything except 'UK' or 'US', then the above expression returns null.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements), and the results you want from this data.

  • How to call the nested in the SQL table collection

    Please see the example below, how to attach the collection vmi_emp with history_emp table

    CREATE or REPLACE type vmi_emp as an object

    (empno varchar2 (500),)

    Ename varchar2 (500),

    HireDate date,

    SAL number (20));

    CREATE or REPLACE type nested_emp is table of the vmi_emp;

    declare

    v_type nested_emp;

    Start

    Select vmi_emp (empno, ename, hiredate, sal)

    bulk collect into v_type from emp;

    ???????????????? Select * from history_emp a, b vmi_emp where a.empno = b.empno

    end loop;

    end;

    Select * from history_emp has, b table (v_type) where a.empno = b.empno

  • How to access objects collection

    How to access the collections of objects for example ReportSections report? I can access ReportSection, but not his collection...

    Reference: http://www.ni.com/pdf/manuals/372844e.pdf

    Maybe you don't understand exactly what section of the report is. According to the help:

    Use this class objects to store, update and transfer reports. You typically use the ReportSection objects to avoid creating copies of a report that the report is transferred between the generator of reports, process, Report and ReportView control object models. You can create a new ReportSection object by calling the Report.NewReportSection method. ReportSection objects can contain other ReportSection objects.

    As a general rule, a process template creates and updates the ReportSection object and assigns to the report object by setting the Report.ReportSection property.

    It has a sub-property called ReportSubsections , which specifies a reference to the ReportSections object, which is a collection that contains a list of child ReportSection objects to the current object. Looking at this object, it's a list of items that you can index through to get each.

  • I have a DAQ Assistant configured to read several channels at the same time. When I have a graphical indicator of wire to the output, I see all my signals mixed together. How I divided them into separate signals?

    I have a DAQ Assistant configured to read 2 channels at the same time. When I have a graphical indicator of wire to the output, I see 2 signals mixed together. How I divided them into separate signals?

    When I wire any type of indicator, it is show that a release of a single channel.

    I want 2 indicators showing 2 different signals as expected from 2 channels configured. How to do this?

    I tried to use split signal but it end by showing that 1 out of 1 signal two indicators.

    Thanks in advance.

    Yes you are right. I tried, but I don't have the result.

    I just find the path. When we launch the split signal, we should expand it (split signal icon) by top, not the bottom. It took me a while to understand this.

    Thank you

  • How to divide the Panel layer in two?

    How do divide you the layers panel in half? I've seen references to do this but Googling this question does not work.

    I use CS2

    Far as I can tell that the person referred to the screenshot of the layers panel appeared in the article, not the real layers panel in Photoshop.

  • How to DIVIDE a TRACK STEREO L &amp; R CHANNELS INTO TWO MONO CHANNELS

    Does anyone know if this is possible: how to DIVIDE a TRACK STEREO L & R CHANNELS INTO TWO MONO CHANNELS?  There must be a way, but I can't find how.

    I recorded a podcast - me and a guest, with each microphone goes to its own mono channel. When I opened it in hearing he mixes in a stereo channel.

    Thanks in advance for any help.

    Best,

    Shawn

    Once you have opened the file in the waveform editing view go to menu Edit "excerpt from the channels to mono files.

  • How to divide the Dates

    Hi all...

    Here, I use the following scripts...

    create table a10 (eno number, date f, date t, number of sal)

    INSERT IN A10 (ENO, F, T, SAL) VALUES)

    1, TO_Date (1 July 2013 12:00:00 AM ',' DD/MM/YYYY HH: mi: SS AM'), TO_Date (June 30, 2014 12:00 ',' DD/MM/YYYY HH: mi: SS AM')

    (100);

    INSERT IN A10 (ENO, F, T, SAL) VALUES)

    1, TO_Date (May 1, 2013 12:00:00 AM ',' DD/MM/YYYY HH: mi: SS AM'), TO_Date (April 30, 2014 12:00 ',' DD/MM/YYYY HH: mi: SS AM')

    (200);

    COMMIT;

    entry:

    ENO      F             T                SAL

    07/01/2013 2014/06/30 100 1

    1 05/01/2013 30/04/2014 200

    Expected results:

    ENO FTSAL

    1 05/01/2013 2013/06/30 200

    1 30/04/2014 300 07/01/2013

    1 05/01/2014 2014/06/30 100

    I'm little bit confused how to divide the dates here... Any help is appreciated.

    Thank you all

    Stéphane

    Hello

    Always tell what version of Oracle you are using, especially if it's so old.

    In Oracle 10 (or 9, by the way) you can do this way:

    WITH cntr AS

    (

    SELECT LEVEL AS n

    OF the double

    CONNECT BY LEVEL<=>

    )

    got_change_date AS

    (

    SELECT d.eno

    C.n

    WHEN 1 THEN f

    ANOTHER t + 1

    END AS change_date

    C.n

    WHEN 1 THEN 1

    OF ANOTHER-1

    END as mul

    sal

    BY a10 d

    CROSS JOIN cntr c

    -WHERE... - If you need any filtering, put it here

    )

    got_total_sal AS

    (

    SELECT eno

    change_date f

    Advance (change_date) OVER (PARTITION BY eno

    ORDER BY change_date

    ) - 1 AS t

    SUM (sal * mul) over (PARTITION BY eno

    ORDER BY change_date

    ) AS total_sal

    OF got_change_date

    )

    SELECT *.

    OF got_total_sal

    WHERE t IS NOT NULL

    ORDER BY eno, f

    ;

  • How to fill a collection?

    Hello

    I need to know how to fill a collection which I will later use to insert into the database. The following is an excerpt from the code I'm writing and please assume that all variables are correct except for the part in question.

    declare

    R_ipm_ip_addr RECORD TYPE IS
    (ip_addr_id, ipm_ip_addr.ip_addr_id%TYPE,
    ip_dec ipm_ip_addr.ip_decimal%TYPE,
    ip_addrr ipm_ip_addr.ip_address%TYPE,
    Comments ipm_ip_addr.comments%TYPE,
    own_orgid ipm_ip_addr.owner_organization_id%TYPE,
    sharing of ipm_ip_addr.sharing%TYPE,
    block_id ipm_ip_addr.block_id%TYPE);

    TYPE t_ipm_ip_addr IS TABLE OF THE r_ipm_ip_addr;
    l_ipm_ip_addr t_ipm_ip_addr: = t_ipm_ip_addr (r_ipm_ip_addr);

    BEGIN

    .......

    FOR indx IN 1... l_ipm_stat_host. Count

    LOOP

    ipm_net_index: = l_ipm_net. FIRST;

    While (ipm_net_index IS NOT NULL)

    LOOP

    IF l_ipm_stat_host (indx) .ip_addr_int .start_ip_int l_ipm_net (ipm_net_index) and l_ipm_net (ipm_net_index) .end_ip_int

    THEN

    dbms_output.put_line (l_ipm_stat_host (indx) .ip_addr |) » --> ' || l_ipm_net (ipm_net_index) .start_ipadd | » /'|| l_ipm_net (ipm_net_index) .cidr_size);

    v_ipnet_ipadd: = l_ipm_net (ipm_net_index) .start_ipadd;

    v_ipnet_cidr: = l_ipm_net (ipm_net_index) .cidr_size;

    OPEN c_get_block_id.

    EXTRACT c_get_block_id INTO v_parent_blkid, v_parent_orgid;

    CLOSE C_get_block_id;

    l_ipm_ip_addr.extend;

    l_ipm_ip_addr (l_ipm_ip_addr.last): = t_ipm_ip_addr (gen_uuid () l_ipm_stat_host (indx) .ip_addr_int, l_ipm_stat_host (indx) .ip_addr, .attr1 l_ipm_stat_host (indx) | l_ipm_stat_host (indx) .attr2, v_parent_orgid, 'sharing', v_parent_blkid); < < <-this is the part that gives me an error (PLS-00306: wrong number or types of arguments in the call to 'T_IPM_IP_ADDR')

    EXIT;

    END IF;

    ipm_net_index: = l_ipm_net. Next (ipm_net_index);

    y_ctr: = y_ctr + 1;

    END LOOP;

    -dbms_output.put_line (l_ipm_stat_host (indx) .ip_addr);

    v_ctr: = v_ctr + 1;

    END LOOP;

    END;

    Help you enjoy! Thank you!

    I got it!

    I added another variable to record and the values assigned to it before passing it to the table. See below for more details.

    declare

    R_ipm_ip_addr RECORD TYPE IS
    (ip_addr_id, ipm_ip_addr.ip_addr_id%TYPE,
    ip_dec ipm_ip_addr.ip_decimal%TYPE,
    ip_addrr ipm_ip_addr.ip_address%TYPE,
    Comments ipm_ip_addr.comments%TYPE,
    own_orgid ipm_ip_addr.owner_organization_id%TYPE,
    sharing of ipm_ip_addr.sharing%TYPE,
    block_id ipm_ip_addr.block_id%TYPE);

    TYPE t_ipm_ip_addr IS TABLE OF THE r_ipm_ip_addr;

    rec_ipm_ip_addr r_ipm_ip_addr;
    l_ipm_ip_addr t_ipm_ip_addr: = t_ipm_ip_addr();

    BEGIN

    .......

    FOR indx IN 1... l_ipm_stat_host. Count

    LOOP

    ipm_net_index: = l_ipm_net. FIRST;

    While (ipm_net_index IS NOT NULL)

    LOOP

    IF l_ipm_stat_host (indx) .ip_addr_int .start_ip_int l_ipm_net (ipm_net_index) and l_ipm_net (ipm_net_index) .end_ip_int

    THEN

    dbms_output.put_line (l_ipm_stat_host (indx) .ip_addr |) » -->'|| l_ipm_net (ipm_net_index) .start_ipadd | » /'|| l_ipm_net (ipm_net_index) .cidr_size);

    v_ipnet_ipadd: = l_ipm_net (ipm_net_index) .start_ipadd;

    v_ipnet_cidr: = l_ipm_net (ipm_net_index) .cidr_size;

    OPEN c_get_block_id.

    EXTRACT c_get_block_id INTO v_parent_blkid, v_parent_orgid;

    CLOSE C_get_block_id;

    l_ipm_ip_addr.extend;

    rec_ipm_ip_addr.ip_addr_id: = gen_uuid();

    rec_ipm_ip_addr.ip_dec: = l_ipm_stat_host (indx) .ip_addr_int;

    rec_ipm_ip_addr. IP_ADDR: = l_ipm_stat_host (indx) .ip_addr;

    rec_ipm_ip_addr.comments: = l_ipm_stat_host (indx) .attr1 | » -'|| l_ipm_stat_host (indx) .attr2 | » -'|| l_ipm_stat_host (indx) .attr3 | » -'|| l_ipm_stat_host (indx) .attr4 | » -'|| .attr5 l_ipm_stat_host (indx);

    rec_ipm_ip_addr.own_orgid: = v_parent_orgid;

    rec_ipm_ip_addr. Sharing: = 'shared ';

    rec_ipm_ip_addr.block_id: = v_parent_blkid;

    l_ipm_ip_addr (l_ipm_ip_addr. (Last): = rec_ipm_ip_addr;

    EXIT;

    END IF;

    ipm_net_index: = l_ipm_net. Next (ipm_net_index);

    y_ctr: = y_ctr + 1;

    END LOOP;

    -dbms_output.put_line (l_ipm_stat_host (indx) .ip_addr);

    v_ctr: = v_ctr + 1;

    END LOOP;

    END;

  • How to set multiple parameters in a sql EXECUTE IMMEDIATE

    I want to set nls_language and nls_date_language and set them as different languages. fnd_global.set_nls_context () does not work. So I think I can use the EXECUTE IMMEDIATE and add them together into a single statement. But I don't know how to make that happen. Can someone help me? Thank you very much.

    PS: It must be done in a single call.

    928091 wrote:
    Hey, thanks for your reply, I forgot to mention that it should be in the begin-end block, do you know how?

    EXEC is a more SQL API which can be used to execute the PL/SQL code. What it does is put the PL/SQL code in a BEGIN block... END of block and run.

    So, you can also manually put code in a BEGIN block... END of block and run like that.

    SQL> begin
      2     execute immediate q'[begin execute immediate 'alter session set nls_date_language = ''AMERICAN'''; execute immediate 'alter session set nls_language = ''AMERICAN'''; end;]';
      3  end;
      4  /
    
    PL/SQL procedure successfully completed.
    
    SQL> 
    

    And I don't understand what possible benefit he made a single run in a BEGIN block... Terminate during execution of multiples.

    Why this code is not in your interest, something like this

    begin
         execute immediate 'alter session set nls_date_language = ''AMERICAN''';
         execute immediate 'alter session set nls_language = ''AMERICAN''';
    end;
    /
    

Maybe you are looking for

  • Read new files only created in the shared folder

    Have an iMac with 2 Configuration of users. admin created a directory/folder (/ data) and in the Get Info gave users read and write access to the folder with 'apply to enclosed... items. ». New documents created by one of the users can not be written

  • dies black screen and computer

    3 months feel like PC all-in-one.   When you watch point on Netflix, with sound, the computer just dies and the screen goes black. Could not turn off the computer via the power button / stop and have to unplug.   This has happened several times and n

  • Latest 'Classic' firmware for EA4500

    Are we still on the same version of the 'roll-back' of 2 years - 2.0.37 (Build 131047)?

  • Download error - cannot download anything on the windows vista computer.

    When I try to download anything, it says that Windows cannot find 'C:\Users\Admin\Downloads\shockwave_Installer_Slim.exe '. Then he said, make sure you typed the name correctly and then try again. I hope you can help me. Original title: download erro

  • LEXAR Jumpdrive V10 8 GB (among other brands) - are not accessible by Win7 Pro 64-bit

    Strange that when unzipped, it load.  About 2 hours later, the light starts to blink all the time and has since been inaccessible (flashes rapidly).  The drive appears by name in the disk drives, with the letter 'F' portable devices and as a "mass st