Cursors in Procedures

hi, i am writing a procedure where initially a cursor stores student ids in c_st_id based on certain conditions. Now on the selected Student ids i have to perform more logic which will filter them again and the filrered student ids are stored in cursor c_nf_st_id . Here i want those student ids that are in the cursor c_st_id but not present in c_nf_st_id. Can anybody suggest me how do i perform subtraction in cursors so that i get those that are in first cursor and not present in second cursor.
CURSOR c_st_id IS
SELECT st_id FROM ci_st_char a,ci_st b
where a.adhoc_char_val in( select value from st_val) and a.prem_id=b.char_prem_id and char_type_cd = 'PLOT# '
and trim(sa_type_cd) in ('stfr',stre,stup,stbo) and st_status_flg in ('20','50')
) where r_num=1;
cr_st_rec c_st_id%ROWTYPE;
OPEN c_st_id;
LOOP
FETCH c_st_id INTO cr_st_rec;
EXIT WHEN c_st_id%NOTFOUND;
BEGIN
CURSOR c_nf_st_id IS
SELECT st_id FROM ci_ft a,ci_adj b where a.st_id = cr_st_rec.st_id and a.parent_id='PIDPLFEE '
and a.sibling_id = b.adj_id and b.adj_status_flg in ('50')
cr_nf_st_rec c_nf_st_id%ROWTYPE;
END LOOP;
CLOSE c_st_id;
Edited by: chesplay on Dec 22, 2011 12:07 AM

chesplay wrote:
I should modify my main query where the st id's are taken and perform the second cursor filter there itself , rather than using two cursors? is my undstanding correct?All SQLs are cursors. So there is no getting away from not using cursors.
The issue is how to effectively use that SQL cursor (and PL/SQL supplies a bunch of interfaces to interact with a SQL cursor).
A SQL select cursor represents the truth at a specific point in time. A simple example.
- At T1 you create a cursor for select * from emp.
- At T2 you fetched 2 of the 100 emp rows.
- At T3 the CEO in a fit of rage, fires all employees and the employee table content is deleted using a delete from emp cursor and the transaction is committed.
- At T4 your code, using your select cursor fetches the next set of rows output by that cursor.
What happens? There are no more rows in the table.
Your cursor sees the truth at T1. And that is what it will return - all 100 rows. Despite the fact that the new truth (version) of that table says that there are no more rows in that table.
So from a basic issue of consistency, you need to realise that a cursor itself is consistent. But when you (in the client side) compare or match results from one cursor with another cursor, these cursors can represent different versions of the truth.
Therefore it is safer to provide both sets of logic for getting the data of interest, and adding the logic for comparing the data of interest, into a single SQL. This is executed as a single cursor - that will look at a single truth and single version of the data of interest.
The added benefits to this is performance and scalability.
There is no need for data having to be pulled from the SQL engine for PL/SQL to process - and often then push back to the very same SQL engine for use via yet another cursor.
Instead of sending SQL data via a detour through the PL/SQL engine to be processed, a single SQL statement is tasked to do this. There's no need for context switching to the PL/SQL engine. No need for the SQL engine to send data to the PL/SQL engine.
SQL can be parallelised. If the data is being processed by PL/SQL, that by default means a single serialised process. No scalability to deal with larger volumes - unless that PL/SQL code is redesigned and rewritten to support some form of (mostly manual) parallel processing.
So data crunching is best done using SQL and using SQL alone. It is the best, most flexible, and most performant and scalable language to crunch database data. Only when the crunching is too complex for the SQL language to deal with, then it makes sense to use PL/SQL to fill that gap.
The basic mantra for using SQL and PL/SQL: maximise SQL and minimise PL/SQL

Similar Messages

  • How return parameter ref Cursor from procedure using dynamic SQL?

    I sorry, but i very need help.
    I using Oracle 8.0.6
    I need to return parameter of type ref Cursor from procedure.
    create or replace package PlanExp is
    type cursortype is ref cursor;
    procedure ShowPlan (cursorparam out
    cursortype.............);
    end PlanExp;
    create or replace package body PlanExp is
    procedure ShowPlan (cursorparam out cursortype,
    .............) Is
    sql_str varchar2(1000);
    sql_str_select varchar2(100);
    sql_str_from varchar2(100);
    sql_str_where varchar2(500);
    Return_Code integer;
    Num_Rows integer;
    cur_id_sel integer;
    tSum_Plan DBMS_SQL.NUMBER_TABLE;
    tSum_Plan_Ch DBMS_SQL.NUMBER_TABLE;
    tSum_Plan_Day DBMS_SQL.NUMBER_TABLE;
    begin
    /* calculating string variables ........... /*
    sql_str := 'select ' || sql_str_select ||
    'from ' || sql_str_from ||
    'where ' || sql_str_where ||
    'group by ' || sql_str_select;
    cur_id_sel := dbms_sql.open_cursor;
    dbms_sql.parse(cur_id_sel, sql_str, dbms_sql.native);
    dbms_sql.define_array(cur_id_sel, 1, tSum_Plan, 20, 1);
    dbms_sql.define_array(cur_id_sel, 2, tSum_Plan_Ch, 20, 1);
    dbms_sql.define_array(cur_id_sel, 3, tSum_Plan_Day, 20, 1);
    Return_Code := dbms_sql.execute(cur_id_sel);
    delete from TEMP_SHOWPLAN;
    Loop
    Num_Rows := dbms_sql.Fetch_Rows(cur_id_sel);
    dbms_sql.column_value(cur_id_sel, 1, tSum_Plan);
    dbms_sql.column_value(cur_id_sel, 2, tSum_Plan_Ch);
    dbms_sql.column_value(cur_id_sel, 3, tSum_Plan_Day);
    if Num_Rows = 0 then
    exit;
    end if;
    Exit When Num_Rows < 20;
    End Loop;
    dbms_sql.close_cursor(cur_id_sel);
    end;
    end PlanExp;
    How return cursor (cursorparam) from 3 dbms_sql.column_value-s ?

    I am using Oracle 8.1.7, so I don't know if this will work in
    8.0.6 or not:
    SQL> CREATE TABLE test
      2    (col1                    NUMBER,
      3     col2                    NUMBER,
      4     col3                    NUMBER)
      5  /
    Table created.
    SQL> INSERT INTO test
      2  VALUES (1,1,1)
      3  /
    1 row created.
    SQL> INSERT INTO test
      2  VALUES (2,2,2)
      3  /
    1 row created.
    SQL> INSERT INTO test
      2  VALUES (3,3,3)
      3  /
    1 row created.
    SQL> CREATE TABLE temp_showplan
      2    (tSum_Plan               NUMBER,
      3     tSum_Plan_Ch            NUMBER,
      4     tSum_Plan_Day           NUMBER)
      5  /
    Table created.
    SQL> EDIT planexp
    CREATE OR REPLACE PACKAGE PlanExp
    IS
      TYPE CursorType IS REF CURSOR;
      PROCEDURE ShowPlan
        (cursorparam    IN OUT CursorType,
         sql_str_select IN     VARCHAR2,
         sql_str_from   IN     VARCHAR2,
         sql_str_where  IN     VARCHAR2);
    END PlanExp;
    CREATE OR REPLACE PACKAGE BODY PlanExp
    IS
      PROCEDURE ShowPlan
        (cursorparam    IN OUT CursorType,
         sql_str_select IN     VARCHAR2,
         sql_str_from   IN     VARCHAR2,
         sql_str_where  IN     VARCHAR2)
      IS
        sql_str                VARCHAR2 (1000);
        cur_id_sel             INTEGER;
        return_code            INTEGER;
      BEGIN
        DELETE FROM temp_showplan;
        sql_str := 'INSERT INTO   temp_showplan '
               || ' SELECT '   || sql_str_select
               || ' FROM '     || sql_str_from
               || ' WHERE '    || sql_str_where;
        cur_id_sel := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE (cur_id_sel, sql_str, DBMS_SQL.NATIVE);
        return_code := DBMS_SQL.EXECUTE (cur_id_sel);
        DBMS_SQL.CLOSE_CURSOR (cur_id_sel);
        OPEN cursorparam FOR SELECT * FROM temp_showplan;
      END ShowPlan;
    END PlanExp;
    SQL> START planexp
    Package created.
    Package body created.
    SQL> VARIABLE g_ref REFCURSOR
    SQL> EXEC PlanExp.ShowPlan (:g_ref, 'col1, col2,
    col3', 'test', ' 1 = 1 ')
    PL/SQL procedure successfully completed.
    SQL> PRINT g_ref
    TSUM_PLAN TSUM_PLAN_CH TSUM_PLAN_DAY
             1            1             1
             2            2             2
             3            3             3

  • How to out Dynamic ref cursor from Procedure to Forms

    Hi
    I am trying to out Dynamic ref cursor from Procedure to Forms, But I am unable to do so. however cursor return the value within procedure but I am failed to capture the same in Forms
    Pl advice suggestion if any, Here I am attaching full procedure for reference
    CREATE PACKAGE winepkg
    IS
    TYPE wine IS RECORD ( mynumber number);
    /* Define the REF CURSOR type. */
    TYPE wine_type IS REF CURSOR RETURN wine;
    END winepkg;
    CREATE procedure find_wine
    (col1_in in number,
    c1 out winepkg.wine_type) as
    vsql varchar2(1000);
    cur sys_refcursor;
    x number;
    BEGIN
    vsql:='select bo_id from bo_details where bo_details.bo_id = '||col1_in ;
    open cur for vsql;
    c1:=cur;
    --fetch c1 into x;
    --dbms_output.put_line(x);
    END find_wine;
    In front end forms
    Declare
    TYPE F is REF CURSOR;
    CUR_F F;
    rec number;
    Begin
    break;
    find_wine( 1601480000011078,cur_f ) ;
    Loop
    fetch cur_f into rec ;
    Message(rec ) ;pause;
    exit when cur_f%notfound ;
    End loop ;
    exception
    when others then
    Message(sqlerrm) ;pause;
    End ;

    yo can use
    declare
    c_cursor EXEC_SQL.CursType;
    v_stmt varchar2(2000) = 'select a, b, c from mytab where cond1'; -- you can create this value dynamically
    begin
    c_cursor := Exec_SQL.Open_cursor;
    EXEC_SQL.PARSE(c_articulos, v_stmt);
    EXEC_SQL.DEFINE_COLUMN(c_articulos,1, v_colchar1, 30);
    EXEC_SQL.DEFINE_COLUMN(c_articulos,2, v_colchar2, 15);
    EXEC_SQL.DEFINE_COLUMN(c_articulos,3, v_colchar3, 30);
    v_exec := EXEC_SQL.EXECUTE(c_cursor);
    WHILE EXEC_SQL.FETCH_ROWS(c_cursor) > 0 LOOP
    EXEC_SQL.COLUMN_VALUE(c_cursor,1,v_colchar1);
    EXEC_SQL.COLUMN_VALUE(c_c_cursor,2,v_colchar2);
    EXEC_SQL.COLUMN_VALUE(c_c_cursor,3,v_colchar3);
    assign_values_to_block;
    END LOOP;
    EXEC_SQL.CLOSE_CURSOR(c_cursor);
    end;
    and WORKS IN FORMS 6

  • How to return cursor from procedure to jdbc

    plz help me through example of code as wl as procedure where.... return cursor from procedure to jdbc

    SET QUOTED_IDENTIFIER OFF
    GO
    SET ANSI_NULLS OFF
    GO
    CREATE procedure anil3 @count INT OUT,@opcode INT OUT,@total_tiff INT OUT
    as
    declare @query2 varchar(300),@tiff_count int,@query1 varchar(300)
    set @query1='declare move_cursor   cursor forward_only static for
    select count(opcode),opcode from TABLE1 group by opcode
    open move_cursor'
    exec(@query1)
    fetch next  from move_cursor into @count,@opcode
    set @opcode="'"+@opcode+"'"
    set @total_tiff=0
    while (@@fetch_status=0)
    begin
         set @query2='declare move_cursor2  cursor static for '+
         ' select count(tiff) from TABLE2  where opcode='+@opcode+
           ' open move_cursor2 '
         exec(@query2)
         fetch next  from move_cursor2 into @tiff_count
         while (@@fetch_status=0)
         begin
              set @total_tiff=@total_tiff+@tiff_count
              fetch next  from move_cursor2 into @tiff_count
         end
         close move_cursor2
         deallocate move_cursor2
    print  @total_tiff
    print @count
    print @opcode
    fetch next  from move_cursor into @count,@opcode
    end
    close move_cursor
    deallocate move_cursor
    SET QUOTED_IDENTIFIER OFF
    GO
    SET ANSI_NULLS ON
    GO******************************************************************************
    above this is sql server 2000 PL/SQL and i hv to get the value
    print @total_tiff
    print @count
    print @opcode
    through JDBC
    plz help me out how to return Cursor to JDBC and HOW toPRINT THESE THREE VALUE @total_tiff, @count, @opcode through JDBC
    get this values through JDBC

  • How to retrieve the max value from a cursor in procedure

    Hi,
    In a procedure, I defined a cursor:
    cursor c_emp is select empno, ename, salary from emp where ename like 'J%';
    but in the body part, I need to retrieve the max(salary) from the cursor.
    could you please tell me how I can get the max value from the cursor in the procedure.
    Thanks,
    Paul

    Here is one sample but you should just get the max directly. Using bulk processing should be a last resort.
    DECLARE
      CURSOR c1 IS (SELECT * FROM emp where sal is not null);
      TYPE typ_tbl IS TABLE OF c1%rowtype;
      v typ_tbl;
      max_sal number;
    BEGIN
      OPEN c1;
      max_sal := -9999999999999;
      LOOP                                                 --Loop added
        FETCH c1 BULK COLLECT INTO v LIMIT 3; -- process 3 records at a time
            -- process the records
           DBMS_OUTPUT.PUT_LINE('Processing ' || v.COUNT || ' records.');
            FOR i IN v.first..v.last LOOP
                 if v(i).sal > max_sal then
                   max_sal := v(i).sal;
                 end if;
                DBMS_OUTPUT.PUT_LINE(v(i).empno);
            END LOOP; 
        EXIT WHEN c1%NOTFOUND;
      END LOOP;
      DBMS_OUTPUT.PUT_LINE('Max salary was: ' || max_sal);
    END;
    Processing 3 records.
    7369
    7499
    7521
    Processing 3 records.
    7566
    7654
    7698
    Processing 3 records.
    7782
    7788
    7839
    Processing 3 records.
    7844
    7876
    7900
    Processing 2 records.
    7902
    7934
    Max salary was: 5000

  • Using Ref cursor from Procedure output in BPEL

    Hi
    Can any body help me ..
    The output variable of db adapter is refcursor from stored procedure. in ref cursor i will get xml from a clob variable. how to use it in bpel...can an body help me how to do it....

    APEX is based on Oracle Database. Whatever you can do with PL/SQL, the same can be done with APEX also. APEX stores the application definition in the form of metadata.
    So if you put all your logic and code in packages, procedures or functions then it will be really good compact and modular approach.
    Bottom line is that you can do all of those.
    Check the documentation at
    http://www.oracle.com/technetwork/developer-tools/apex/documentation/index.html
    Thanks,
    Mehabub

  • Using Ref Cursor in procedure in place of normal cursor

    Hi,
    I have a procedure where i have the follwoing cursors declared,
    CURSOR c_wip IS
    SELECT tdvalue
    FROM tabledetails
    WHERE tdidcode = 'PEL_DASHBOARD'
    AND tdkey LIKE 'WIP_QUEUES%';
    CURSOR c_pending IS
    SELECT tdvalue
    FROM tabledetails
    WHERE tdidcode = 'PEL_DASHBOARD'
    AND tdkey LIKE 'PENDING_QUEUES%';
    CURSOR c_out IS
    SELECT tdvalue
    FROM tabledetails
    WHERE tdidcode = 'PEL_DASHBOARD'
    AND tdkey LIKE 'OUT_QUEUES%';
    CURSOR c_out_status IS
    SELECT tdvalue
    FROM tabledetails
    WHERE tdidcode = 'PEL_DASHBOARD'
    AND tdkey LIKE 'OUT_STATUS%';
    Will the usage of 1 ref cursor instead of the 4 cursors increase the performance.
    Thanks.

    Cursor is a structure which points to a memory locations.
    While a Ref-cursor is a datastructure which point to a object which inturn points to Memory locations.
    The advantage of having Ref-cursor is that we can pass dynamically the Ref-cursor as a parameter to a procedures.
    So it does improve the performance to a small extent.

  • Using Ref cursor in Procedure.

    Hi All,
    Can i use a single ref cursor multple times within the life cycle of a procedure.
    Thanks,
    Dillip

    Yes.
    See the example here. A cursor expression is selected - repeatedly, within a loop - into emp_cur (which is of type REF CURSOR) - and emp_cur is then used to process it in a nested loop.
    (By the way - this question would be better asked in the PL/SQL).
    Regards Nigel

  • Union select as cursor in procedure

    Hi!
    With ref. to an earlier post from Sept. 11, 2009, I am facing the following problem:
    In my procedure I have this statement as a cursor:
    SELECT a.carrier_code, a.flight_no, a.from_city, a.origin, a.dept_time, a.to_city, a.destination, a.arr_time, a.flight_date,
    a.aircraft_type, a.booking_class, a.service_class, a.num_of_seats, a.checked_bag, a.fare_basis, a.fare_type, a.currency, a.rt_net_fare, a.tax, a.surcharges, a.fare_total
    FROM favailability a, main_related_flight b
    WHERE a.flight_leg_id = b.flight_leg_id_1
    AND a.from_city = 'London'
    AND a.service_class = 'Business'
    AND a.fare_type = 'Business Saver'
    AND a.flight_date = '27.09.09'
    UNION
    SELECT d.carrier_code, d.flight_no, d.from_city, d.origin, d.dept_time, d.to_city, d.destination, d.arr_time, d.flight_date,
    d.aircraft_type, d.booking_class, d.service_class, d.num_of_seats, d.checked_bag, d.fare_basis, d.fare_type, d.currency, d.rt_net_fare, d.tax, d.surcharges, d.fare_total
    FROM favailability d, main_related_flight e
    WHERE d.flight_leg_id = e.flight_leg_id_2
    AND d.from_city = 'Zurich'
    AND d.service_class = 'Business'
    AND d.fare_type = 'Business Flex'
    AND d.flight_date = '03.10.09'
    ORDER BY flight_date;
    ARRIER_CODE FLIGHT_NO              FROM_CITY                                                              ORIGIN DEPT_TIME TO_CITY                                                                DESTINATION ARR_TIME FLIGHT_DATE               AIRCRAFT_TYPE                            BOOKING_CLASS SERVICE_CLASS                  NUM_OF_SEATS           CHECKED_BAG          FARE_BASIS FARE_TYPE                      CURRENCY RT_NET_FARE            TAX                    SURCHARGES             FARE_TOTAL            
    LX           345                    London                                                                 LHR    06:00     Zurich                                                                 ZRH         08:40    27.09.09                  Airbus A320                              J             Business                       64                     30 kg                LXJ        Business Saver                 EUR      337                    30,01                  35,24                  402,25                
    LX           450                    Zurich                                                                 ZRH    07:00     London                                                                 LCY         07:35    03.10.09                  AVRO RJ100                               Z             Business                       37                     30 kg                LXZ        Business Flex                  EUR      740                    30,01                  21,24                  791,25                
    2 rows selectedworks fine when running the statement alone. However, when running the procedure, my exception appears:
    ORA-20022: Fare not found for flight from London to Zurich on 27-Sep-2009
    Please note that the fare type can be different for inbound and outbound flights.
    PROCEDURE  FLIGHT (p_ptc_adult IN NUMBER,
                      p_ptc_adult_rt       IN NUMBER,
                      p_ptc_adult_add      IN NUMBER,
                      p_ptc_adult_add_rt   IN NUMBER,
                      p_city_o             IN favailability.from_city%TYPE,
                      p_city_d             IN favailability.to_city%TYPE,
                      p_service_class      IN favailability.service_class%TYPE,
                      p_fare_type          IN favailability.fare_type%TYPE,
                      p_fare_type_rt       IN favailability.fare_type%TYPE,
                      p_flightdate         IN favailability.flight_date%TYPE,
                      p_flightdate_rt      IN favailability.flight_date%TYPE,
                      p_username           IN users.username%TYPE,
                      p_password           IN users.password%TYPE,
                      p_card_type          IN users_cc.card_type%TYPE,
                      p_creditcardnumber   IN VARCHAR2,
                      p_expiry_date        IN DATE,
                      p_first_name_add     IN VARCHAR2,
                      p_last_name_add      IN VARCHAR2) IS
    CURSOR c1 IS
    SELECT a.carrier_code, a.flight_no, a.from_city, a.origin, a.dept_time, a.to_city, a.destination, a.arr_time, a.flight_date,
    a.aircraft_type, a.booking_class, a.service_class, a.num_of_seats, a.checked_bag, a.fare_basis, a.fare_type, a.currency, a.rt_net_fare, a.tax, a.surcharges, a.fare_total
    FROM favailability a, main_related_flight b
    WHERE a.flight_leg_id = b.flight_leg_id_1
    AND a.from_city = p_city_o
    AND a.service_class = p_service_class
    AND a.fare_type = p_fare_type
    AND a.flight_date = p_flightdate
    UNION
    SELECT d.carrier_code, d.flight_no, d.from_city, d.origin, d.dept_time, d.to_city, d.destination, d.arr_time, d.flight_date,
    d.aircraft_type, d.booking_class, d.service_class, d.num_of_seats, d.checked_bag, d.fare_basis, d.fare_type, d.currency, d.rt_net_fare, d.tax, d.surcharges, d.fare_total
    FROM favailability d, main_related_flight e
    WHERE d.flight_leg_id = e.flight_leg_id_2
    AND d.from_city = p_city_d
    AND d.service_class = p_service_class
    AND d.fare_type = p_fare_type_rt
    AND d.flight_date = p_flightdate_rt
    ORDER BY flight_date;
    f_rec                 c1%ROWTYPE;
    v_num_of_seats        favailability.num_of_seats%TYPE;
    v_fare_type           favailability.fare_type%TYPE;
    v_fare_type_rt        favailability.fare_type%TYPE;
    v_net_fare            favailability.rt_net_fare%TYPE;
    v_currency            fl_itinerary_t.currency_t%TYPE;
    v_rid                 fl_itinerary_t.rid%TYPE;
    v_status              fl_itinerary_t.status%TYPE;
    v_ptc_adult           NUMBER;
    v_ptc_adult_rt        NUMBER;
    v_ptc_adult_add       NUMBER;
    v_ptc_adult_add_rt    NUMBER;
    e_no_passenger        EXCEPTION;
    e_no_available_fares  EXCEPTION;
    e_no_user             EXCEPTION;
    e_credit_card_expired EXCEPTION;
    v_error_code          error_log.err_code%TYPE;
    v_error_message       error_log.err_message%TYPE;
    v_error_date          error_log.err_date%TYPE;
    v_user_name           users.username%TYPE;
    v_password            users.password%TYPE;
    v_user_id             users.user_id%TYPE;
    v_first_name          users.first_name%TYPE;
    v_last_name           users.last_name%TYPE;
    v_dob                 users.dob%TYPE;
    v_country_code        users.country_code%TYPE;
    v_prefix              users.prefix%TYPE;
    v_mobile_phone        users.mobile_phone%TYPE;
    v_card_type           users_cc.card_type%TYPE;
    v_creditcardnumber    users_cc.card_number%TYPE;
    v_expiry_date         DATE;
    v_grand_total         NUMBER(10,2);
    v_first_name_add      VARCHAR2(40);
    v_last_name_add       VARCHAR2(40);
    v_booking_code        VARCHAR2(6);
    v_result              VARCHAR2(32);
    BEGIN
    v_ptc_adult        := p_ptc_adult;
    v_ptc_adult_rt     := p_ptc_adult_rt;
    v_ptc_adult_add    := p_ptc_adult_add;
    v_ptc_adult_add_rt := p_ptc_adult_add_rt;
    -- Check user input
        IF p_city_o = p_city_d THEN
         dbms_output.put_line ('Departure city cannot be arrival city!');
        ELSIF p_flightdate = p_flightdate_rt THEN
         dbms_output.put_line ('Departure date cannot be arrival date!');
        ELSIF p_flightdate > p_flightdate_rt THEN
         dbms_output.put_line ('Departure date cannot be after arrival date!');
        ELSIF p_flightdate < sysdate OR p_flightdate_rt < sysdate THEN
         dbms_output.put_line ('Departure and arrival date cannot be in the past!');
        ELSE
        IF nvl(trunc(p_ptc_adult), 0) = 0
        OR nvl(trunc(p_ptc_adult_rt), 0) = 0
        THEN
            RAISE e_no_passenger;
        END IF;
    -- Check outbound availability
        SELECT num_of_seats INTO v_num_of_seats
        FROM favailability
        WHERE v_ptc_adult = p_ptc_adult
        AND v_ptc_adult_add = p_ptc_adult_add
        AND from_city = p_city_o
        AND to_city = p_city_d
        AND service_class = p_service_class
        AND fare_type = p_fare_type
        AND fare_type = p_fare_type_rt
        AND flight_date = p_flightdate;
        IF p_ptc_adult > v_num_of_seats
         OR v_num_of_seats < 2 THEN
          dbms_output.put_line ('No seats available!');
        ELSE
         UPDATE favailability SET num_of_seats = num_of_seats - p_ptc_adult
         WHERE v_ptc_adult = p_ptc_adult
         AND from_city = p_city_o
         AND to_city = p_city_d
         AND service_class = p_service_class
         AND fare_type = p_fare_type
         AND fare_type = p_fare_type_rt
         AND flight_date = p_flightdate;
        END IF;
        IF p_ptc_adult_add > v_num_of_seats
         OR v_num_of_seats < 2 THEN
          dbms_output.put_line ('No seats available!');
        ELSE
         UPDATE favailability SET num_of_seats = num_of_seats - p_ptc_adult_add
         WHERE v_ptc_adult_add = p_ptc_adult_add
         AND from_city = p_city_o
         AND to_city = p_city_d
         AND service_class = p_service_class
         AND fare_type = p_fare_type
         AND fare_type = p_fare_type_rt
         AND flight_date = p_flightdate;
        END IF;
    -- Check inbound availability
        SELECT num_of_seats INTO v_num_of_seats
        FROM favailability
        WHERE v_ptc_adult_rt = p_ptc_adult_rt
        AND v_ptc_adult_add_rt = p_ptc_adult_add_rt
        AND from_city = p_city_d
        AND to_city = p_city_o
        AND service_class = p_service_class
        AND fare_type = p_fare_type_rt
        AND fare_type = p_fare_type
        AND flight_date = p_flightdate_rt;
        IF p_ptc_adult_rt > v_num_of_seats
         OR v_num_of_seats < 2 THEN
         dbms_output.put_line ('No seats available!');
        ELSE
         UPDATE favailability SET num_of_seats = num_of_seats - p_ptc_adult_rt
         WHERE v_ptc_adult_rt = p_ptc_adult_rt
         AND from_city = p_city_d
         AND to_city = p_city_o
         AND service_class = p_service_class
         AND fare_type = p_fare_type_rt
         AND fare_type = p_fare_type
         AND flight_date = p_flightdate_rt;
        END IF;
        IF p_ptc_adult_add_rt > v_num_of_seats
         OR v_num_of_seats < 2 THEN
         dbms_output.put_line ('No seats available!');
        ELSE
         UPDATE favailability SET num_of_seats = num_of_seats - p_ptc_adult_add_rt
         WHERE v_ptc_adult_add_rt = p_ptc_adult_add_rt
         AND from_city = p_city_d
         AND to_city = p_city_o
         AND service_class = p_service_class
         AND fare_type = p_fare_type_rt
         AND fare_type = p_fare_type
         AND flight_date = p_flightdate_rt;
        END IF;
      -- get credit info
        SELECT u.user_id, u.first_name, u.last_name, u.dob, u.country_code, u.prefix, u.mobile_phone, p_card_type, p_creditcardnumber, p_expiry_date
           INTO   v_user_id, v_first_name, v_last_name, v_dob, v_country_code, v_prefix, v_mobile_phone, v_card_type, v_creditcardnumber, v_expiry_date
           FROM   dual, users u, users_cc c
           WHERE  u.user_id = c.user_id
           AND u.username = p_username
           AND u.password = p_password
           AND c.card_type = p_card_type
           AND c.card_number = p_creditcardnumber
           AND c.expiry_date = p_expiry_date;
           IF SQL%ROWCOUNT = 0 THEN
                              RAISE e_no_user;
           END IF;
           IF p_expiry_date < sysdate THEN
                  RAISE e_credit_card_expired;
           END IF;
        v_result := booking_pkg.validatecreditcard(p_creditcardnumber);
    -- open cursor
    OPEN c1;
    LOOP
        FETCH c1 INTO f_rec;
        EXIT WHEN c1%notfound;
    -- insert records    
         INSERT INTO fl_itinerary_t (rid, carrier_t, fno_t, from_city_t, origin_t, dept_t, to_city_t, destination_t, arr_t, fdate_t, aircraft_type_t, booking_class_t, service_class_t,
                                     num_of_seats_t, checked_bag_t, fare_basis_t, fare_type_t, currency_t, fare_t, tax_t, surcharges_t, fare_total_t, grand_total_t, trans_date,
                                     status, user_id, first_name, last_name, dob, country_code, prefix, mobile_phone)
         VALUES (new_res_seq.nextval, f_rec.carrier_code, f_rec.flight_no, f_rec.from_city, f_rec.origin, f_rec.dept_time, f_rec.to_city, f_rec.destination, f_rec.arr_time, f_rec.flight_date, f_rec.aircraft_type, f_rec.booking_class,
                 f_rec.service_class, p_ptc_adult + p_ptc_adult_add, f_rec.checked_bag, f_rec.fare_basis, f_rec.fare_type, f_rec.currency, f_rec.rt_net_fare, f_rec.tax, f_rec.surcharges, f_rec.fare_total, f_rec.fare_total * (p_ptc_adult + p_ptc_adult_add), sysdate,
                 'Confirmed', v_user_id, v_first_name, v_last_name, v_dob, v_country_code, v_prefix, v_mobile_phone);
      -- additional traveller
         SELECT p_first_name_add, p_last_name_add
         INTO v_first_name_add, v_last_name_add
         FROM dual;
         IF v_ptc_adult_add = p_ptc_adult_add
         AND v_ptc_adult_add_rt = p_ptc_adult_add_rt
         AND v_first_name_add = p_first_name_add
         AND v_last_name_add = p_last_name_add
         THEN
           INSERT INTO fl_itinerary_add (trans_id, carrier, fno, from_city, to_city, fdate, first_name_2, last_name_2, dob_2, main_user_id, trans_date, status)
           VALUES (new_trans_seq.nextval, f_rec.carrier_code, f_rec.flight_no, f_rec.from_city, f_rec.to_city, f_rec.flight_date, v_first_name_add, v_last_name_add, null, v_user_id, sysdate, 'Confirmed');
         END IF;
      COMMIT;  
    END LOOP;
    CLOSE c1;
         -- show itinerary for main traveller
           dbms_output.put_line ('Itinerary completed!');
           dbms_output.put_line (v_ptc_adult || ' seat(s) reserved for ' || v_first_name ||', ' || v_last_name);
           dbms_output.put_line (v_ptc_adult_rt || ' seat(s) reserved for ' || v_first_name ||', ' || v_last_name);
         -- show itinerary for 2.traveller
           dbms_output.put_line (v_ptc_adult_add || ' seat(s) reserved for ' || v_first_name_add ||', ' || v_last_name_add);
           dbms_output.put_line (v_ptc_adult_add_rt || ' seat(s) reserved for ' || v_first_name_add ||', ' || v_last_name_add); 
    END IF;
           -- Create new booking
           INSERT INTO booking (booking_id,
                                rid,
                                e_ticket_no,
                                booking_code,
                                user_id,
                                first_name,
                                last_name,
                                dob,
                                credit_card_type,
                                credit_card_number,
                                currency, 
                                booking_date,
                                status)
           VALUES (new_booking_seq.nextval,
                   new_res_seq.currval,
                   dbms_random.value(1000000000000, 9999999999999),
                   dbms_random.string('X', 6), 
                   v_user_id,
                   v_first_name,
                   v_last_name,
                   v_dob,
                   v_card_type,
                   'xxxx-xxxx-xxxx-'||substr(v_creditcardnumber,-4),
                   f_rec.currency,
                   SYSDATE, 'Confirmed');
            SELECT booking_code
            INTO v_booking_code
            FROM booking;
             dbms_output.put_line ('Booking code: ' || v_booking_code);
    EXCEPTION
        WHEN e_no_available_fares THEN
          RAISE_APPLICATION_ERROR (-20021, 'There are no fares available for flight'||
                                           ' from '|| p_city_o||' to '|| p_city_d||' on '||TO_CHAR(p_flightdate, 'dd-Mon-yyyy'));
        WHEN no_data_found THEN
          RAISE_APPLICATION_ERROR (-20022, 'Fare not found for flight from '|| p_city_o||
                                           ' to '|| p_city_d||' on '||TO_CHAR(p_flightdate, 'dd-Mon-yyyy'));
        WHEN too_many_rows THEN
          RAISE_APPLICATION_ERROR (-20023, 'More than one fare found for flight from '||
                                            p_city_o||' to '|| p_city_d||' on '||
                                            TO_CHAR(p_flightdate, 'dd-Mon-yyyy'));
        WHEN e_no_passenger THEN
          RAISE_APPLICATION_ERROR(-20001, 'Please enter a valid number of travelers!');
        WHEN e_no_user THEN
          RAISE_APPLICATION_ERROR(-20002, 'User not found!');
        WHEN e_credit_card_expired THEN
          RAISE_APPLICATION_ERROR(-20003, 'Credit card has expired!');
        --WHEN OTHERS THEN
        --  v_error_code := SQLCODE;
        --  v_error_message := SUBSTR(SQLERRM, 1, 200);
         -- INSERT INTO error_log (err_code, err_message, err_date) VALUES
          --  (v_error_code, v_error_message, sysdate);
    END FLIGHT;Furthermore, only 1 booking can be inserted into the booking table.

    Hi!
    Thanks for the tip! Now inserting into the booking table works! Sorry for bothering again. In my package I have this function that should check the
    credit card number. Works alone, but not within the package (procedure). Please see above procedure too.
    FUNCTION VALIDATECREDITCARD (p_creditcardnumber IN VARCHAR2)
       RETURN VARCHAR2
    IS
       creditcardnumber    VARCHAR2 (32);
                --:= nosymbols (p_CreditCardNumber, LENGTH (p_CreditCardNumber));
       creditcardlength    NUMBER         := LENGTH (p_creditcardnumber);
       subtotal            NUMBER         := 0;
       t_value             NUMBER         := 0;
       c1                  NUMBER;
       c2                  NUMBER;
       c3                  NUMBER;
       c4                  NUMBER;
       cardtype            VARCHAR2 (160) := 'CARD';
       calculationmethod   VARCHAR2 (160) := 'UNKNOWN';
       RESULT              VARCHAR2 (160);
       v_expiry_date       DATE;
    BEGIN
       creditcardnumber := LTRIM(RTRIM(p_creditcardnumber));
       creditcardnumber := REPLACE(creditcardnumber, '-');
       creditcardnumber := REPLACE(creditcardnumber, '.');
    -- IF isnumber (CreditCardNumber) = 0 THEN
       c1 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 1));
       c2 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 2));
       c3 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 3));
       c4 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 4));
       IF creditcardlength = 13
       THEN
          IF c1 IN (4)
          THEN
             cardtype := 'VISA';
             calculationmethod := 'MOD10';
          END IF;
       ELSIF creditcardlength = 14
       THEN
          IF c2 IN (36, 38)
          THEN
             cardtype := 'DINERS CLUB';
             calculationmethod := 'MOD10';
          ELSIF c3 IN (300, 301, 302, 303, 304, 305)
          THEN
             cardtype := 'DINERS CLUB';
             calculationmethod := 'MOD10';
          END IF;
       ELSIF creditcardlength = 15
       THEN
          IF c2 IN (34, 37)
          THEN
             cardtype := 'AMEX';
             calculationmethod := 'MOD10';
          ELSIF c4 IN (2014, 2149)
          THEN
             cardtype := 'enROUTE';
             calculationmethod := 'ANY';
          ELSIF c4 IN (2131, 1800)
          THEN
             cardtype := 'JBC';
             calculationmethod := 'MOD10';
          END IF;
       ELSIF creditcardlength = 16
       THEN
          IF c1 IN (4)
          THEN
             cardtype := 'VISA';
             calculationmethod := 'MOD10';
          ELSIF c1 IN (3)
          THEN
             cardtype := 'JBC';
             calculationmethod := 'MOD10';
          ELSIF c2 IN (51, 52, 53, 54, 55)
          THEN
             cardtype := 'MASTERCARD';
             calculationmethod := 'MOD10';
          ELSIF c4 IN (6011)
          THEN
             cardtype := 'DISCOVER';
             calculationmethod := 'MOD10';
          END IF;
       END IF;
       IF calculationmethod = 'MOD10'
       THEN
          FOR i IN REVERSE 1 .. LENGTH (creditcardnumber)
          LOOP
                 IF cardtype = 'AMEX'
             THEN
                IF (TO_NUMBER (SUBSTR (TO_CHAR (i), LENGTH (i), 1)) NOT IN  (1, 3, 5, 7, 9))
                THEN
                   t_value := SUBSTR (creditcardnumber, i, 1) * 2;
                   subtotal := subtotal + SUBSTR (t_value, 1, 1);
                   subtotal := subtotal + NVL (SUBSTR (t_value, 2, 1), 0);
                ELSE
                   subtotal := subtotal + SUBSTR (creditcardnumber, i, 1);
                END IF;                       
             ELSE          
                IF (TO_NUMBER (SUBSTR (TO_CHAR (i), LENGTH (i), 1)) IN  (1, 3, 5, 7, 9))
                THEN
                   t_value := SUBSTR (creditcardnumber, i, 1) * 2;
                   subtotal := subtotal + SUBSTR (t_value, 1, 1);
                   subtotal := subtotal + NVL (SUBSTR (t_value, 2, 1), 0);
                ELSE
                   subtotal := subtotal + SUBSTR (creditcardnumber, i, 1);
                END IF;                       
             END IF;
          END LOOP;
          IF MOD (subtotal, 10) = 0
          THEN
             RESULT := 'VALID';
          ELSE
             RESULT := 'INVALID';
          END IF;
       ELSIF calculationmethod = 'ANY'
       THEN
          RESULT := 'VALID';
       ELSE
          RESULT := 'UNKNOWN';
       END IF;
       RESULT := RESULT || ', ' || cardtype;
       RETURN (RESULT);
    END VALIDATECREDITCARD;
    create or replace PACKAGE        "BOOKING_PKG"
    AS
      PROCEDURE  FLIGHT (p_ptc_adult IN NUMBER,
                      p_ptc_adult_rt       IN NUMBER,
                      p_ptc_adult_add      IN NUMBER,
                      p_ptc_adult_add_rt   IN NUMBER,
                      p_city_o             IN favailability.from_city%TYPE,
                      p_city_d             IN favailability.to_city%TYPE,
                      p_service_class      IN favailability.service_class%TYPE,
                      p_fare_type          IN favailability.fare_type%TYPE,
                      p_fare_type_rt       IN favailability.fare_type%TYPE,
                      p_flightdate         IN favailability.flight_date%TYPE,
                      p_flightdate_rt      IN favailability.flight_date%TYPE,
                      p_username           IN users.username%TYPE,
                      p_password           IN users.password%TYPE,
                      p_card_type          IN users_cc.card_type%TYPE,
                      p_creditcardnumber   IN VARCHAR2,
                      p_expiry_date        IN DATE,
                      p_first_name_add     IN VARCHAR2,
                      p_last_name_add      IN VARCHAR2);
      PROCEDURE  NEW_USER (p_username      IN users.username%TYPE,
                           p_password      IN users.password%TYPE,
                           p_salutation       users.salutation%TYPE,
                           p_academic_title   users.academic_title%TYPE,
                           p_first_name       users.first_name%TYPE,
                           p_last_name        users.last_name%TYPE,
                           p_dob              users.dob%TYPE,
                           p_street_address   users.street_address%TYPE,
                           p_company_name     users.company_name%TYPE,
                           p_street_address_2 users.street_address_2%TYPE,
                           p_country          users.country%TYPE,
                           p_postal_code      users.postal_code%TYPE,
                           p_city             users.city%TYPE,
                           p_e_mail_address   users.e_mail_address%TYPE,
                           p_country_code     users.country_code%TYPE,
                           p_prefix           users.prefix%TYPE,
                           p_mobile_phone     users.mobile_phone%TYPE,
                           p_home_phone       users.home_phone%TYPE,
                           p_language         users.language%TYPE,
                           p_newsletter       users.newsletter%TYPE,
                           p_specials         users.specials%TYPE,
                           p_membership       users.membership%TYPE,
                           p_remarks          users.remarks%TYPE);
      FUNCTION  VALIDATECREDITCARD (p_creditcardnumber IN VARCHAR2) RETURN VARCHAR2;
    END BOOKING_PKG;Thanks for your help!

  • Query on cursors in procedures

    hi, I have been trying to pass a tablename as a parameter in a procedure. This tablename I then need to use in a cursor .
    my code is something like this.
    create procedure abc (tablename varchar2)
    is
    cursor xyz is select rowid from tablename;
    But for some reason I can't access the tablename from that cursor. I get an error saying
    PL/SQL: SQL Statement ignored
    PL/SQL: ORA-00942: table or view does not exist.
    Please assist

    Hi,
    you can use rowid type for the array. Moreover, you can use dynamic sql with bulk collect option. For instance:
    oradev> select * from v$version;
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.6.0 - Production
    PL/SQL Release 9.2.0.6.0 - Production
    CORE 9.2.0.6.0 Production
    TNS for Solaris: Version 9.2.0.6.0 - Production
    NLSRTL Version 9.2.0.6.0 - Production
    oradev> create table a1(c1 int);
    Table created.
    oradev> insert into a1 select rownum from all_objects;
    34466 rows created.
    oradev> create or replace procedure proc1 (table_name varchar2, num_rows out int)
    2 as
    3 type tr is table of rowid index by binary_integer;
    4 vs varchar2(1000);
    5 arr tr;
    6 begin
    7 vs := 'select rowid from '||table_name;
    8 execute immediate vs bulk collect into arr;
    9 num_rows := arr.count;
    10 end;
    11 /
    Procedure created.
    oradev> var n number
    oradev> exec proc1('A1', :n)
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:00.06
    oradev> print :n
    N
    34466
    Andrey

  • Error PLS-00355 occurs when open cursor in procedure

    Hello everybody,
    I have a problen that really drives me mad:
    Whenever I try to compile the following procedure, the Error "PLS-00355: use of pl/sql table not allowed in this context" occurs but I can't see what is wrong. I hope the wisdome of the community will help to solve the problem.
    Here is the code:
    CREATE or REPLACE PROCEDURE data_year(study_year IN NUMBER) AS
    sep CONSTANT CHAR := ';';
    CURSOR cursor_year(year_of_interest NUMBER) IS
    select
    BBS_CONDITIONS.COUNTRYNUM as country,
    BBS_CONDITIONS.STATENUM as state,
    BBS_CONDITIONS.ROUTE as route,
    BBS_ROUTES.LATITUDE as lat,
    BBS_ROUTES.LONGITUDE as lon,
    BBS_SUMMARY.AOU as aou,
    BBS_SPECIES.ENGLISH_COMMON_NAME as name,
    BBS_SPECIES.MIGRATION_TYPE as migtype,
    BBS_SUMMARY.SPECIESTOTAL as count,
    BBS_CONDITIONS.MONTH as smonth,
    BBS_CONDITIONS.STARTTIME as starttime,
    BBS_CONDITIONS.ENDTIME as endtime,
    BBS_CONDITIONS.RPID as rpid,
    BBS_CONDITIONS.ASSISTANT as assistant
    from BBS_SPECIES,
    BBS_ROUTES,
    BBS_SUMMARY,
    BBS_CONDITIONS
    where BBS_ROUTES.ROUTE = BBS_CONDITIONS.ROUTE
    and BBS_ROUTES.STATENUM = BBS_CONDITIONS.STATENUM
    and BBS_ROUTES.COUNTRYNUM = BBS_CONDITIONS.COUNTRYNUM
    and BBS_SPECIES.AOU = BBS_SUMMARY.AOU
    and BBS_CONDITIONS.YEAR = BBS_SUMMARY.YEAR
    and BBS_CONDITIONS.ROUTE = BBS_SUMMARY.ROUTE
    and BBS_CONDITIONS.STATENUM = BBS_SUMMARY.STATENUM
    and BBS_CONDITIONS.COUNTRYNUM = BBS_SUMMARY.COUNTRYNUM
    and BBS_SPECIES.CLASS_ID = 0
    and BBS_CONDITIONS.YEAR = year_of_interest
    order by country, state, route, migtype, aou;
    rec_year cursor_year%ROWTYPE;
    i NUMBER := 1;
    TYPE table_year_type IS TABLE OF cursor_year%ROWTYPE;
    table_year table_year_type;
    BEGIN -- Begin of procedure data_year
    table_year := table_year();
    OPEN cursor_year(study_year);
    LOOP
    FETCH cursor_year into rec_year;
    EXIT WHEN cursor_year%NOTFOUND;
    table_year.EXTEND(1);
    table_year(i) := rec_year;
    i := i+1;
    END LOOP;
    CLOSE cursor_year;
    DBMS_OUTPUT.PUT_LINE('country' || sep || 'state' || sep || 'route' || sep || 'lat' || sep || 'lon'
    || sep || 'aou' || sep || 'name' || sep || 'migtype' || sep || 'count' || sep || 'month' || sep
    || 'starttime' || sep || 'endtime' || sep || 'rpid' || sep || 'assistant');
    FOR i in table_year.FIRST..table_year.LAST LOOP
    DBMS_OUTPUT.PUT_LINE(TO_CHAR(table_year(i).country) || sep || TO_CHAR(table_year(i).state) ||
    sep || TO_CHAR(table_year(i).route) || sep || TO_CHAR(table_year(i).lat) || sep ||
    TO_CHAR(table_year(i).lon) || sep || TO_CHAR(table_year(i).aou) || sep ||
    table_year(i).name || sep || TO_CHAR(table_year(i).migtype) || sep ||
    TO_CHAR(table_year(i).count) || sep || TO_CHAR(table_year(i).smonth) || sep ||
    TO_CHAR(table_year(i).starttime) || sep || TO_CHAR(table_year(i).endtime) || sep ||
    TO_CHAR(table_year(i).rpid) || sep || TO_CHAR(table_year(i).assistant));
    END LOOP;
    END; -- End of procedure data_year
    Thanks ahead, TC

    Does it give a line number? I'm wondering if it might be
    table_year := table_year();
    that's causing the problem. Try removing it and see.
    Even better, change your code to
       TYPE table_year_type IS TABLE OF cursor_year%ROWTYPE INDEX BY BINARY_INTEGER;
       table_year     table_year_type;
    BEGIN        
        OPEN cursor_year (study_year);
        FETCH cursor_year BULK COLLECT INTO table_year;
        CLOSE cursor_year;
    ...Edited by: Dave Hemming on Nov 21, 2008 9:51 AM

  • Two Cursor in Procedure

    I was trying to use two cursors in a procedure and while compiling in DBA Studio, I get the following error: "Line # = 19 Column # = 14 Error Text = PLS-00341: declaration of cursor 'RECORD_ID_CURSOR' is incomplete or malformed"
    Are you not supposed to have two cursors in a procedure? Please help. Thanks.
    Paul

    PLS-00341 declaration of cursor 'string' is incomplete or malformed
    this is the error you are getting. You can have 2 cursors but the way it's defined is incorrect. Check your spellings and the way it's defined.

  • Pass REF CURSOR to Procedure and LOOP through it

    Hi All,
    I am trying to figure out how I can pass a ref cursor to a procedure and then loop through it. I have provided an example of what I am attempting to do...just not really sure how to open the ref cursor when it is passed ot the sproc and iterate through it?
    Any info would be greatly appreciated.
    Version:
    Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
    PL/SQL Release 10.2.0.3.0 - Production
    CORE     10.2.0.3.0     Production
    TNS for Linux: Version 10.2.0.3.0 - Production
    NLSRTL Version 10.2.0.3.0 - Production
    Create Or Replace Package test_ref_pkg
    AS
    function get_ref_curs
    RETURN SYS_REFCURSOR;
    procedure process_ref_curs(
    p_ref_cursor        IN  SYS_REFCURSOR
    END test_ref_pkg;
    Create Or Replace Package Body test_ref_pkg
    AS
    function get_ref_curs
        RETURN SYS_REFCURSOR
    IS
        l_ref_curs      SYS_REFCURSOR;
    BEGIN
        OPEN l_ref_curs FOR
        Select 1 ID, 'Test 1' Name
        From DUAL
        UNION ALL
        Select 2 ID, 'Test 2' Name
        From DUAL;
    END get_ref_curs;
    procedure process_ref_curs(
    p_ref_cursor        IN  SYS_REFCURSOR
    IS
    BEGIN
        ---NOT SURE WHAT TO DO TO ITERATE THROUGH THE REF CURSOR I AM PASSING INTO THIS SPROC?----
    END process_ref_curs;
    END test_ref_pkg;Thanks,
    S
    Edited by: ScarpacciOne on May 28, 2010 9:11 AM

    ---NOT SURE WHAT TO DO TO ITERATE THROUGH THE REF CURSOR I AM PASSING INTO THIS SPROC?----
    -- MAYBE I AM SIMPLE, BUT HOW ABOUT FETCH???
    -- if you start to yell, I will yell too!!!!
    Sybrand Bakker
    Senior Oracle DBA

  • How to declare cursor in procedure based on if condition?

    Hi Experts,
    In sql server I have eprocedure in which I declare cursor like this:
    IF (@int_cntCondition = 1 )
    BEGIN
    DECLARE Date_Cursor CURSOR FOR select HolidayCcy,HolidayDate from Definition..HolidayCalendar WITH (NOLOCK) where
    HolidayCcy in (@Deposit_Currency,@Alternate_Currency)
    AND CONVERT(SMALLDATETIME,CONVERT(VARCHAR(25),HolidayDate,106)) >=
    CONVERT(SMALLDATETIME,CONVERT(VARCHAR (25),@T_Date,106))
    END
    ELSE
    BEGIN
    DECLARE Date_Cursor CURSOR FOR select HolidayCcy,HolidayDate from Definition..HolidayCalendar WITH (NOLOCK) where
    HolidayCcy in (@Deposit_Currency,@Alternate_Currency,@Bank_Base_Currency)
    AND CONVERT(SMALLDATETIME,CONVERT(VARCHAR(25),HolidayDate,106)) >=
    CONVERT(SMALLDATETIME,CONVERT(VARCHAR(25),@T_Date,106))
    END
    I have to declare same cursor in oracle based on 'if' condition.
    But in oracle stored procedur cursor has to declare outside of Begin statment of procedure, so how can I declare This cursor in Orracle?
    if anyone know about it, Plese help or send any link to refer.
    Thanks.

    Digambar wrote:
    I have to declare same cursor in oracle based on 'if' condition.The simple answer is to use a reference cursor data type. E.g.
    SQL> create or replace procedure GetObjects( cur in out sys_refcursor, objType varchar2 ) is
      2  begin
      3          case
      4                  when upper(objType) = 'EMP' then
      5                          open cur for select * from emp;
      6 
      7                  when upper(objType) = 'DEPT' then
      8                          open cur for select * from dept;
      9 
    10          end case;
    11  end;
    12  /
    Procedure created.
    SQL>
    SQL>
    SQL> --// define a host refcursor variable in client
    SQL> --// (e.g. VB, .Net, Java, etc)
    SQL> var c refcursor
    SQL>
    SQL> --// make the stored proc call
    SQL> begin GetObjects( :c, 'EMP' ); end;
      2  /
    PL/SQL procedure successfully completed.
    SQL>
    SQL> --// process cur reference in client
    SQL> print c
         EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
          7369 SMITH      CLERK           7902 1980/12/17 00:00:00        800                    20
          7499 ALLEN      SALESMAN        7698 1981/02/20 00:00:00       1600        300         30
          7521 WARD       SALESMAN        7698 1981/02/22 00:00:00       1250        500         30
          7566 JONES      MANAGER         7839 1981/04/02 00:00:00       2975                    20
          7654 MARTIN     SALESMAN        7698 1981/09/28 00:00:00       1250       1400         30
          7698 BLAKE      MANAGER         7839 1981/05/01 00:00:00       2850                    30
          7782 CLARK      MANAGER         7839 1981/06/09 00:00:00       2450                    10
          7788 SCOTT      ANALYST         7566 1987/04/19 00:00:00       3000                    20
          7839 KING       PRESIDENT            1981/11/17 00:00:00       5000                    10
          7844 TURNER     SALESMAN        7698 1981/09/08 00:00:00       1500          0         30
          7876 ADAMS      CLERK           7788 1987/05/23 00:00:00       1100                    20
          7900 JAMES      CLERK           7698 1981/12/03 00:00:00        950                    30
          7902 FORD       ANALYST         7566 1981/12/03 00:00:00       3000                    20
          7934 MILLER     CLERK           7782 1982/01/23 00:00:00       1300                    10
    14 rows selected.
    SQL>
    SQL>
    SQL> --// make the stored proc call
    SQL> begin GetObjects( :c, 'DEPT' ); end;
      2  /
    PL/SQL procedure successfully completed.
    SQL>
    SQL> --// process cur reference in client
    SQL> print c
        DEPTNO DNAME          LOC
            10 ACCOUNTING     NEW YORK
            20 RESEARCH       DALLAS
            30 SALES          CHICAGO
            40 OPERATIONS     BOSTON
    SQL>

  • URGENT!!passing variables to ref-cursor stored procedures

    I have build forms6 block base ona stored procedures with ref
    cursor.
    But....it's impossible to pass variables from block.item to
    in-out parameter of ref cursor..to make a where clause for
    example!!
    I've tried all..but nothing happens..
    Can someone help me?
    Thanks..
    null

    Manish Wadhwa (guest) wrote:
    : Gigi (guest) wrote:
    : : I have build forms6 block base ona stored procedures with ref
    : : cursor.
    : : But....it's impossible to pass variables from block.item to
    : : in-out parameter of ref cursor..to make a where clause for
    : : example!!
    : : I've tried all..but nothing happens..
    : : Can someone help me?
    : : Thanks..
    : >>
    : It is not possible to send values as parameter to the stored
    : procedure because, oracle uses the trigger query-procedure for
    : calling the stored procedure and it does not entertain any
    : changes to that trigger. This is a problem with block based on
    : stored procedure, we have also tried it with table of records
    : instead of ref cursor, but doesn't work.
    : Manish
    Thanks Manish..
    i was afraid about that..
    But ..i ask to myself(retoric question..) it's possible the
    development oracle team build a "black box" like a stored
    procedure with ref o table of records who permit to select
    million of records in few second( i selected 5 million of records
    in 2 seconds..) and cannot permit to passing variables to these
    cursor.. every end users production forms must be working in
    this way..I don't understand..
    Gigi
    null

Maybe you are looking for