How to write a procedure using collections

how can we define collections inside procedure and use it
i am getting an error executing this proc
create or replace procedure p_collections
is
type calendar is varray(366) of  date;
calendar c1;
begin
for i in 1 .. 100
loop
select sysdate bulk collect into c1 from dual;
end loop;
DBMS_OUTPUT.PUT_LINE(c1);
end;Edited by: Rahul_India on Sep 12, 2012 2:54 PM
Edited by: Rahul_India on Sep 12, 2012 3:07 PM

That's because you only have one value in the array.
Here is sample code that you can test in the SCOTT schema.
set serveroutput on;
declare
  cursor my_cur is
  select empno from emp;
  type my_type is table of emp.ename%type;
  type my_type2 is table of emp.empno%type;
  dizi my_type;
  dizi2 my_type2;
  query VARCHAR2(100);
begin
  open my_cur;
  fetch my_cur bulk collect into dizi2;
  close my_cur;
  for i in dizi2.first..dizi2.last
  loop
    dbms_output.put_line(dizi2(i));
  end loop;
end;
/Or another sample using a LIMIT clause
The FETCH does a BULK COLLECT of all data into 'v'. It will either get all the data or none if there isn't any.
The LOOP construct would be used when you have a LIMIT clause so that Oracle would 'loop' back to
get the next set of records. Run this example in the SCOTT schema and you will see how the LIMIT clause works.
I have 14 records in my EMP table.
DECLARE
  CURSOR c1 IS (SELECT * FROM emp);
  TYPE typ_tbl IS TABLE OF c1%rowtype;
  v typ_tbl;
BEGIN
  OPEN c1;
  LOOP                                                 --Loop added
    FETCH c1 BULK COLLECT INTO v LIMIT 3; -- process 3 records at a time
        -- process the first 3 max records
       DBMS_OUTPUT.PUT_LINE('Processing ' || v.COUNT || ' records.');
        FOR i IN v.first..v.last LOOP
            DBMS_OUTPUT.PUT_LINE(v(i).empno);
        END LOOP; 
    EXIT WHEN c1%NOTFOUND;
  END LOOP;
  DBMS_OUTPUT.PUT_LINE('All done');
END;In the FOR loop you would do any processing of the nested table you want to do
and could use a FORALL to do an INSERT into another table.

Similar Messages

  • How to write a file using mod pl/sql

    hi,
    i am having a submit button in my procedure. which should inturn create .sql file in a file path.
    is there any way to create a fileusing htp and htf methods.
    Thanks in advance
    Hari

    >
    i am having a submit button in my procedure. which should in turn create .sql file in a file path.
    is there any way to create a file using htp and htf methods.
    >
    Why are you wasting your time coding from scratch using the PL/SQL Web Toolkit instead of the APEX framework?
    From Re: how to write a file using mod pl/sql it appears that you are not using APEX, so a number of the approaches APEX offers are not relevant. You appear to be looking for a file download solution using the <tt>wpg_docload.download_file</tt> method, such as:
    create or replace procedure download_file (
        p_filename  in     varchar2
      , p_mimetype  in     varchar2
      , p_content   in out nocopy blob)
    is
    begin
      -- Set up HTTP header.
      -- Use "application/octet" as default MIME type.
      owa_util.mime_header(nvl(p_mimetype, 'application/octet'), false);
      -- Set the size so the browser knows how much to download.
      htp.p('Content-length: ' || dbms_lob.getlength(p_content));
      -- Filename will be used as default by the browser in "Save as..."
      htp.p('Content-Disposition: attachment; filename="' || p_filename || '"');
      -- Close header.
      owa_util.http_header_close();
      -- Stream the file content to the browser.
      wpg_docload.download_file(p_content);
    end download_file;

  • How to write a procedure to run the call the custom package from backend

    Hi All
    Oracle 10g
    Oracle Apps R12
    I am working with oracle order management here we have a customize Package called (Pick Release).Due to some problem we have running this concurrent program by manually giving Route_id as parameter. The route_id is taken from the route Table. By using this query
    select distinct route_id from route@DB_LINK_APPS_TO_ROADSHOW
    where trunc(route_date) = trunc (sysdate+2).
    so daily we have nearly 42 routes and we are running this concurrent program manually nearly times.
    so now how to write a procedure for this
    Step 1 Getting the route from route table.( By cursor we can get the route_id Accordingly)
    Step 2 How to trigger the custom package from back end and execute accordingly to that output of the cursor(route_id)
    If the cursor get 40 routes is it then the concurrent program runs 40 times according to that route_id.
    can some could provide the steps to do this
    Thanks & Regards
    Srikkanth.M

    This is about 4 or 5 lines of PL/SQL and the name of the custom package is not provided.
    If you request someone in this forum to do your work for free -because obviously you didn't even try to write it, which must be considered abusing this forum- you must at least provide sufficient info so someone can do it.
    And no, I won't do it for you.
    Sybrand Bakker
    Senior Oracle DBA

  • How to write a procedure

    hii am new to oracle , can u help me how to write a procedure for the following query
    Transfer Funds between Accounts:
    Table: AccountMaster (AcctNo (primary key), AcctHolderName, currentBal
    Data: 1, 'A', 1000
    2, 'B', 3000
    3, 'C', 4000
    Function: getBalance (AcctNo)
    Stored Proc: tranferFunds (FromAcctNo IN, ToAcctNo IN, Amt IN, result OUT)
    The proc should use the function getBalance (AcctNo) to find out the current balance for account.
    The proc should return a value of 1 if the transfer was successful.
    It should return a value of -1 if the funds are not sufficient in the fromAccount to transfer.
    Thanx ,
    Raj0412

    I would start by asking your teacher to clarify the parts of the assignment that are not clear to you.
    You may also want to read some books.
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/toc.htm
    http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/toc.htm

  • How to write a application using WDJ with the adobe form ?

    Hi, experts,
    I don't know how to write a application using !!webdynpro for java!! with the adobe form so that I can fill data to the adobe form and get data from the adobe form.
    Note: I have configed the ADS(adobe document services),and I can create a application with a interactiveform in webdynpro for abap and run it successfully, so that I can  fill data to the adobe form and get data from the adobe form.
    Do you give me some hint?
    Thanks a lot.
    Best regards,
    tao
    Edited by: wang tao on Sep 9, 2008 8:59 AM

    Hi,
    Refers the following links.
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/4fd2d690-0201-0010-de83-b4fa0c93e1a9
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/5f27e290-0201-0010-ff82-c21557572da1
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/70c9a954-aa20-2a10-64bb-ea0835204e03
    Thanks
    Abhilasha.

  • How to write a procedure for update the table

    Hi all
    can any body please tell me how to write a procedure to update the table......
    I have a table with about 10,000 records...........Now I have add a new column and I want to add values for that like
    registration Code Creidits
    13213 BBA
    1232 MCS
    I had add the creidit now i want to update the table.........the new value want to get by SQL like
    Creidit = select creidit from othere_table...........
    Hope u can understand my problem
    Thanks in advance
    Regards
    Shayan
    [email protected]

    Please try the following --
    update Program_reg a
    set TotalCreidit = ( select tot_cr <Accroding to your logic>
                                from Program_reg b
                                where a.Registration = b.Registration
                                and    a.Enrollment = b.Enrollment
                                and    a.code = b.code
    where a.Registration in ( select distinct Registration
                                        from Program_reg );
    N.B.: Not Tested....
    Regards.
    Satyaki De.

  • How to write SELECT statement using tables ekko,ekpo and eket?

    Hi,
    I got a problem in  performance tuning using below tables?
    how to write SELECT statement using tables EKKO,EKPO and EKET and in conditon ( WHERE clause)  use only fields 
                        ekko~ebeln       IN ebeln
                       ekko~loekz       EQ ' '
                       ekko~lifnr       IN lifnr
                       ekko~ekorg       IN ekorg
                      ekko~ekgrp       IN ekgrp          
                       ekpo~werks       IN werks
                       ekpo~pstyp       EQ  '3'
                       ekpo~loekz       EQ  space
                       ekpo~elikz       EQ  space
                       ekpo~menge       NE  0
                     eket~rsnum       NE space.
    Thanks in Advance.
    bye.

    Hi,
    ekko~ebeln IN ebeln
    ekko~loekz EQ ' '
    ekko~lifnr IN lifnr
    ekko~ekorg IN ekorg
    ekko~ekgrp IN ekgrp
    ekpo~werks IN werks
    ekpo~pstyp EQ '3'
    ekpo~loekz EQ space
    ekpo~elikz EQ space
    ekpo~menge NE 0          " Remove this from where clause
    eket~rsnum NE space.    " Remove this from where clause
    ' instead delete the entries after fetching into the table
    DELETE it_itab WHERE menge EQ '0' AND rsnum EQ ' '.
    Regards
    Bala Krishna

  • How to execute a procedure with collection passed as parameter?

    i have created the collection:
    CREATE TYPE typ_Project AS OBJECT( project_no NUMBER(2), title VARCHAR2(35), cost NUMBER(7,2))
    CREATE TYPE typ_ProjectList AS VARRAY (50) OF typ_Project
    and a procedure:
    CREATE OR REPLACE PROCEDURE add_project (
    p_deptno IN NUMBER,
    p_new_project IN typ_Project,
    p_position IN NUMBER )
    IS
    v_my_projects typ_ProjectList;
    BEGIN
    SELECT projects INTO v_my_projects FROM department
    WHERE dept_id = p_deptno FOR UPDATE OF projects;
    v_my_projects.EXTEND;
    FOR i IN REVERSE p_position..v_my_projects.LAST - 1 LOOP
    v_my_projects(i + 1) := v_my_projects(i);
    END LOOP;
    v_my_projects(p_position) := p_new_project; -- add new
    UPDATE department SET projects = v_my_projects
    WHERE dept_id = p_deptno;
    END add_project;
    Now please explain how to call this procedure with collection passed as parameter . . .

    For example:
    BEGIN
        add_project(
                    10, -- department number
                    typ_Project(
                                99, -- project number
                                'New Project', -- project title
                                99999.99 -- project cost
                               ), -- new project
                    5 -- project position
    END;
    /SY.

  • How to write such procedure?

    I have a DML trigger on a table. whenever there is a change to the table, the old records will be written to another backup table created before. I want to implement this for many tables in the database. Basically, I want a procedure that takes table name as an input and creats a backup table and a trigger for the input table.
    How to write such procedure?
    thanks.

    ...a simple block to get you started...not tested:
    declare
      t varchar2(64) := 'my_table'; --your table name as a parameter
    begin
      execute immediate 'create table '  || t || '_bck as select * from user_tables where 1 = 0';
      -- thinking you'd create the trigger on your base table, not the backup
      execute immediate 'create trigger ' || t || '_dml_trigger before delete or insert or update on ' || t || ' referencing...you get the point
    end;Hope this helps. If you're not the DBA you may want to discuss this train of thought with him/her.
    Cheers.

  • How to write a procedure to load the data into a table using xml file as input to the procedure?

    Hi,
    Iam new to the xml,
    can u please anyone help me how to write procedure to load the data into a table using xml as input parameter to a procedure and xml file is as shown below which is input to me.
    <?xml version="1.0"?>
    <DiseaseCodes>
    <Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    <Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    <Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    </DiseaseCodes>.
    Regards,
    vikram.

    here is the your XML parse in 11g :
    select *
      from xmltable('//Entity' passing xmltype
    '<?xml version="1.0"?>
    <DiseaseCodes>
    <Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    <Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    <Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity>
    </DiseaseCodes>
    ') columns
      "dcode" varchar2(4000) path '/Entity/dcode',
      "ddesc" varchar2(4000) path '/Entity/ddesc',
      "reauthflag" varchar2(4000) path '/Entity/reauthflag'
    dcode                                                                            ddesc                                                                            reauthflag
    0                                                                                (I87)Other disorders of veins - postphlebitic syndrome                           0
    0                                                                                (J04)Acute laryngitis and tracheitis                                             0
    0                                                                                (J17*)Pneumonia in other diseases - whooping cough                               0
    SQL>
    Using this parser you can create procedure as
    SQL> create or replace procedure myXMLParse(x clob) as
      2  begin
      3    insert into MyXmlTable
      4      select *
      5        from xmltable('//Entity' passing xmltype(x) columns "dcode"
      6                      varchar2(4000) path '/Entity/dcode',
      7                      "ddesc" varchar2(4000) path '/Entity/ddesc',
      8                      "reauthflag" varchar2(4000) path '/Entity/reauthflag');
      9    commit;
    10  end;
    11 
    12  /
    Procedure created
    SQL>
    SQL>
    SQL> exec myXMLParse('<?xml version="1.0"?><DiseaseCodes><Entity><dcode>0</dcode><ddesc>(I87)Other disorders of veins - postphlebitic syndrome</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity><Entity><dcode>0</dcode><ddesc>(J04)Acute laryngitis and tracheitis</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity><Entity><dcode>0</dcode><ddesc>(J17*)Pneumonia in other diseases - whooping cough</ddesc><claimid>34543></claimid><reauthflag>0</reauthflag></Entity></DiseaseCodes>');
    PL/SQL procedure successfully completed
    SQL> select * from MYXMLTABLE;
    dcode                                                                            ddesc                                                                            reauthflag
    0                                                                                (I87)Other disorders of veins - postphlebitic syndrome                           0
    0                                                                                (J04)Acute laryngitis and tracheitis                                             0
    0                                                                                (J17*)Pneumonia in other diseases - whooping cough                               0
    SQL>
    SQL>
    Ramin Hashimzade

  • How to write a procedure to retrieve data in a collection from a table?

    create or replace type Subject as object(
    subject_id number,
    subject_Desc varchar2(50)
    create or replace type Subjects_All as varray(10) of Subject
    create table student(
    StudentID varchar(10),
    Subjects Subjects_all
    so how do i write a procedure to display info such as this:
    Subjects:
    101 'Maths'
    102 'Science'
    Subjects:
    I wrote a procedure like the following but it didn't quite work :(
    CREATE OR REPLACE PROCEDURE StudentLoop AS
    all_subj subjects_all;
    each_subj subject;
    CURSOR all_subjectsrow IS select * from student;
    BEGIN
    FOR all_subj IN all_subjectsrow LOOP
    FOR each_subj IN all_subj LOOP
    DBMS_OUTPUT.PUT_LINE('Last row selected has ID ' || each_subj.subject_id || each_subj.subject_desc );
    END LOOP;
    END LOOP;
    END StudentLoop ;
    Any Advice?

    Hello,
    1.) Don't store Types in Tables. It will become a nightmare when you want to query your data any other way then retrieving your object types. Just don't do it.
    2.) A more relational model is much easier. Considering students and subjects is a n:n relation (a student can have more subjects and a subject can have more students) this might look like this:
    SQL> r
      1  declare
      2    cursor cStudents is
      3      with student as (
      4        select 1 student_id, 'test1' name from dual
      5        union all
      6        select 2 student_id, 'test2' name from dual
      7      ),
      8      subjects as (
      9        select 1 subject_id, 'Maths' subject_desc from dual
    10        union all
    11        select 2 subject_id, 'Science' subject_desc from dual
    12      ),
    13      student_subjects as (
    14        select 1 student_id, 1 subject_id from dual
    15        union all
    16        select 1 student_id, 2 subject_id from dual
    17        union all
    18        select 2 student_id, 2 subject_id from dual
    19      )
    20      select a.name name, b.subject_desc subject
    21      from student a, subjects b, student_subjects c
    22      where a.student_id = c.student_id
    23      and b.subject_id = c.subject_id;
    24  begin
    25    for rS in cStudents loop
    26      dbms_output.put_line('Student '||rS.name||': '||rS.subject);
    27    end loop;
    28* end;
    Student test1: Maths
    Student test2: Science
    Student test1: Sciencecheers

  • How to write stored procedures

    hi
    how you will write stored procedures in jdbc .could u pls tell me the procedure?what is key in jdbc where you will use that in which situation?could ugive me the answer briefly
    thanks
    nag

    Hi
    A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures (sometimes called a sproc or SP) are actually stored in the database.
    Stored procedures are similar to user-defined functions (UDFs).
    A stored procedure is a group of SQL statements that form a logical unit and perform a particular task, and they are used to encapsulate a set of operations or queries to execute on a database server
    Use this
    http://www.ics.com/support/docs/dx/1.5/tut6.html
    http://java.sun.com/docs/books/tutorial/jdbc/basics/sql.html
    http://www.sqlteam.com/article/stored-procedures-an-overview
    Thanks

  • How to write Block ALV using Object-oriented methodology?

    Hi all,
    How to write a Block ALV without using 'Reuse_ALV.....' FMs? I want to use the CL_GUI....' methods. What is the procedure? Any sample block ALV?
    Thanks,
    Charles.

    Currently there is not an OO method of implementing the block ALV, it is only available using the REUSE function module.
    Regards,
    RIch Heilman

  • How to write a log using abap mapping

    Hi all.
    in PI 7.1 environment I need to use abap mapping and I wish to write some XML data into a table that I created for logging the data.
    I know that using the abap mapping I can parse an XML file. My question is how to write this table defining a specific method, if it is necessary.
    Any help or suggestion is well appreciated.
    Many thanks in advance for your kind cooperation.
    Regards,
      Giovanni

    hi,
    >> My question is how to write this table defining a specific method, if it is necessary.
    just like to normal table (insert statement)
    parse XML and get the data you need and just insert into the DB table
    there are many tutorials showing how to parse xml file inside abap mapping
    so just do a little search on sdn
    Regards,
    Michal Krawczyk

  • How to view stored procedure using SQLPlus

    I have created the following stored procedure in Oracle, I would like to know how to view this stored procedure using SQLPlus:
    CREATE PROCEDURE get_product_code(prod_no IN VARCHAR2) IS
         CURSOR Getno IS
              <my_query_goes_here>
         BEGIN
              For Getno_cur IN Getno LOOP
              DBMS_OUTPUT.PUT_LINEGetno_cur.name);
              END LOOP;
         END;
    run;

    Hi,
    You can also use USER_SOURCE in place of ALL_SOURCE, for getting your own procedure code (or any pl/sql schema object).
    Regards

Maybe you are looking for

  • My VGA adapter is not working on my iPod.. Can anyone help?

    I have recently bought a apple iPod to VGA adapter for my iPod touch 4th generation but it is not appearing on the screen or registering it. The device looks like it is registering it as a sound device because the sound control disapears when plugged

  • Develop a report to migrate territories and its attributes

    Hi All, My requirement -> create territories, assign BP and attributes to it through a custom report. Using BAPI BAPI_TERRITORY_CREATE I manage to create territories but assign BP and Attributes i did not find any FM and BAPI. kindly guide me how i c

  • Insert query for insert all data into table in vb6 but it insert 1 row in table

    This is My insert query in vb6 but it insert 1 row in table But i want insert all data in the table which contain the id =1. Note that billtabsuport is blank i want solution for this strSQL = " select * from billtabsuport1 where StockID=" & lblid.Cap

  • Built-In Domain Level Groups dont have permissions on domain they should on 2012

    Hello, First this is a brand new domain environment with everything running server 2012 datacenter edition. Second I've never seen anything like the following occur in a domain environment. What I had is what appears to be a bad 2012 AD structure how

  • Photoshop Elements 6 complete meltdown

    My PSE6 has worked quite well for a couple of years but has now had a complete meltdown. It all started when I imported a batch of images (including CR2 files) from a CF card -- like I've done countless times -- and found that no thumbnails were gene