Looping a query without cursor

Hi everybody
i want to know that is there is any way by which we can loop through Resultset of a query without using cursor for loop in plsql
vipul

No. Reason:
Any SQL statement in PL/SQL is a cursor. Any rows "obtained" from a cursor, is fetched via a read-cursor-loop mechanism.
This can be an explicit cursor, where you open the cursor and manually code the loop with the FETCH statement.
It can be an implicit cursor, where you simply code something like [SELECT col INTO var FROM table] - in which case PL/SQL still creates a cursor and still uses a cursor fetch to read the result of that cursor.
PL/SQL is no different than Java or Delphi or C in this aspect. It takes a SQL statement and deliver that to the SQL Engine. The SQL Engine deals with SQL statements as cursors. It returns a cursor handle to the caller. The caller then has to fetch the rows from the cursor.
What makes PL/SQL different is that the PL/SQL language and compiler integrates so tightly with SQL, that you can mix two different programming languages in the same source code. Unlike Java, Delpi and C, you can code native SQL inside your PL/SQL code. The PL/SQL compiler is clever enough to know what it can execute, and which source lines are SQL that need to be "wrapped and delivered" to the SQL Engine.
Question though - why do you ask for another method? What do you view as a problem with using a cursor loop to fetch rows?

Similar Messages

  • How to write a SQL Query without using group by clause

    Hi,
    Can anyone help me to find out if there is a approach to build a SQL Query without using group by clause.
    Please site an example if is it so,
    Regards

    I hope this example could illuminate danepc on is problem.
    CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
    CREATE OR REPLACE FUNCTION GET_ARR return my_array
    as
         arr my_array;
    begin
         arr := my_array();
         for i in 1..10 loop
              arr.extend;
              arr(i) := i mod 7;
         end loop;
         return arr;
    end;
    select column_value
    from table(get_arr)
    order by column_value;
    select column_value,count(*) occurences
    from table(get_arr)
    group by column_value
    order by column_value;And the output should be something like this:
    SQL> CREATE or replace TYPE MY_ARRAY AS TABLE OF INTEGER
      2  /
    Tipo creato.
    SQL>
    SQL> CREATE OR REPLACE FUNCTION GET_ARR return my_array
      2  as
      3   arr my_array;
      4  begin
      5   arr := my_array();
      6   for i in 1..10 loop
      7    arr.extend;
      8    arr(i) := i mod 7;
      9   end loop;
    10   return arr;
    11  end;
    12  /
    Funzione creata.
    SQL>
    SQL>
    SQL> select column_value
      2  from table(get_arr)
      3  order by column_value;
    COLUMN_VALUE
               0
               1
               1
               2
               2
               3
               3
               4
               5
               6
    Selezionate 10 righe.
    SQL>
    SQL> select column_value,count(*) occurences
      2  from table(get_arr)
      3  group by column_value
      4  order by column_value;
    COLUMN_VALUE OCCURENCES
               0          1
               1          2
               2          2
               3          2
               4          1
               5          1
               6          1
    Selezionate 7 righe.
    SQL> Bye Alessandro

  • CPU Cost of a Query without executing it?

    Is there any mathematical formula to find CPU cost of a query without executing it? Thanks

    902181 wrote:
    Is there any mathematical formula to find CPU cost of a query without executing it? ThanksAny why do you want the cost? What do you expect it to tell you? How are you planning to apply this in your code? Make sure you understand what cost tells you and what it does not tell you. And use that value in a sane fashion.
    As for an alternative to using EXPLAIN PLAN, you can simply parse a SQL into a cursor without executing it. Then determine the SQL cursor id and address and use DBMS_XPLAN to display its execution plan.
    E.g.
    SQL> create or replace procedure ParseSQL( sqlStatement varchar2 ) is
      2          c       integer;
      3  begin
      4          c := DBMS_SQL.open_cursor;
      5 
      6          DBMS_SQL.parse(
      7                  c,
      8                  sqlStatement,
      9                  DBMS_SQL.native
    10          );
    11 
    12          DBMS_SQL.close_cursor( c );
    13  end;
    14  /
    Procedure created.
    SQL>
    SQL> var sqlID varchar2(100)
    SQL> var childNumber number
    SQL> var sqlText varchar2(4000)
    SQL> begin
      2          ParseSQL( 'select /* TEST1 */  * from emp' );
      3 
      4          select
      5                  sql_text, sql_id, child_number into :sqlText, :sqlID, :childNumber
      6          from    v$sql
      7          where   sql_text like 'select /* TEST1 */%';
      8  end;
      9  /
    PL/SQL procedure successfully completed.
    SQL>
    SQL> col PLAN_TABLE_OUTPUT format a50
    SQL> select
      2          rownum, p.plan_table_output
      3  from       TABLE( DBMS_XPLAN.Display_Cursor( :sqlID, :childNumber, 'BASIC,COST' ) ) p;
        ROWNUM PLAN_TABLE_OUTPUT
             1 EXPLAINED SQL STATEMENT:
             2 ------------------------
             3 select /* TEST1 */  * from emp
             4
             5 Plan hash value: 3956160932
             6
             7 -----------------------------------------------
             8 | Id  | Operation         | Name | Cost (%CPU)|
             9 -----------------------------------------------
            10 |   0 | SELECT STATEMENT  |      |     3 (100)|
            11 |   1 |  TABLE ACCESS FULL| EMP  |     3   (0)|
            12 -----------------------------------------------
            13
    13 rows selected.
    SQL> The problem is extracting intelligence from the output as it is raw text. Perhaps not that complex as a where clause can be added to only display line id 0 and then extract the cost value from that line. Regular expressions (not my forte) will likely do this easily.

  • Add loops to Arrange without sync Pitch and Tempo

    I recently purchased a package of Audio loops. This package contained audio files in wav and AIFF format. I tried to add the AIFF loops to Loop browser and Logic but it didn't work. The pitch of the loops would change as I dragged them toward the arranment. So I removed them from the loop browser and tried to use the wav files dragging them from the File Folder and when the only to get the same results. It's strange because when I play them on itunes or other programs they're in the right pitch, but as soon as I transfer them to logic the pitch changes.
    How can I use these loops in Logic without changing the pitch?

    If they are Apple loops...
    Use the aiff versions.. and select the region created when you added the loop... Now use transpose in the Region Inspector on the left of the Arrange window..  to set the loop to play at the correct pitch for your piece.
    If they are not Apple loops.. use the transpose function as described above and then stretch/flex the region to fit the tempo of your piece.

  • Needs  help to retrive the last row in a  select query without using rownum

    Hi ,
    i need to retrive the last row from the select sub query without using rownum.
    is there any other way to retrive the last row other than the below query.
    is that the ROWNUM=1 will always retrive the 1 row of the select query ?
    select from*
    *(select ename from employee where dept_id=5 order by desc) where rownum=1;*
    Please advise.
    thanks for your help advance,
    regards,
    Senthur

    957595 wrote:
    Actually my problem is ithat while selecting the parents hiearchy of the child data using
    CONNECT BY PRIOIR query
    I need the immediate parent of my child data.
    For example my connect BY query returns
    AAA --- ROOT
    BBB --PARENT -2
    CCC --PARENT-1
    DDD IS my input child to the connect by query
    Immediate parent of my child data "DDD" ---> CCC(parent -1)
    i want the data "CCC" from the select query,for that i am taking the last row of the query with rownum.
    I got to hear that using ROWNUM to retrive the data will leads to some problem.It is a like a magic number.I am not sure what the problem will be.
    So confusing with using this rownum in my query.
    Please advice!!!It's not quite clear what you're wanting, but perhaps this may help?
    you can select the PRIOR values to get the parent details if you want...
    SQL> ed
    Wrote file afiedt.buf
      1  select empno, lpad(' ',(level-1)*2,' ')||ename as ename, prior empno as mgr
      2  from emp
      3  connect by mgr = prior empno
      4* start with mgr is null
    SQL> /
         EMPNO ENAME                                 MGR
          7839 KING
          7566   JONES                              7839
          7788     SCOTT                            7566
          7876       ADAMS                          7788
          7902     FORD                             7566
          7369       SMITH                          7902
          7698   BLAKE                              7839
          7499     ALLEN                            7698
          7521     WARD                             7698
          7654     MARTIN                           7698
          7844     TURNER                           7698
          7900     JAMES                            7698
          7782   CLARK                              7839
          7934     MILLER                           7782
    14 rows selected.(ok, not the best of examples as the mgr is already known for a row, but it demonstrates you can select prior data)

  • How to validate a Query without executing it?

    How to validate a Query without executing it?
    For example if I have:
    ReadAllQuery myQuery = new ReadAllQuery(myClass);
    myQuery.setSelectionCriteria(myExpression);
    What can I do to have must validation as possible been done before executing the query?
    Validation that could be done are:
    - Expression reference fields that really exist in TopLink mapping
    - Literal provided to expression are of compatible type
    - Parameter setting set against the query are all compatible

    Your could prepare the query.
    query.prepareCall(session, record);
    This will do all query validation and generate the SQL for the query.

  • Can I rewrite the following query without using Row_number() function ??!!

    Hello every one, can I rewrite the following query without using the 'ROW_NUMBER() OVER ' part.
    The query is supposed to pull out the records whose CODE is not NULL and has most
    recent date for UPDATE_DATE . The reason I wanted to do this is, When I embed this query
    in between many other queries along with JOINs, My oracle server is unable to execute. So, I thought
    its better to supplant 'ROW_NUMBER() OVER ' logic with something else and try it. .
    SELECT a.* FROM
    (SELECT b.*, ROW_NUMBER() OVER (PARTITION BY b.PIDM
    ORDER BY b.UPDATE_DATE DESC) AS Rno
    FROM
    SELECT *
    FROM SHYNCRO WHERE CODE IS NOT NULL
    )b
    )a
    WHERE a.Rno = 1

    Hi,
    You didn't write over 150 lines of code and then start testing it, did you?
    Don't.
    Take baby steps. Write as little as pssiblem test that. Debug and test again until you have something that does exactly what you want it to do.
    When you have somehting that works perfectly, take one baby step. Add a tiny amount of code, maybe 1 or 2 lines more, and test again.
    When you do get an error, or wrong results, you'll have a much better idea of where the problem is. also, you won't be building code on a flimsy foundation.
    If you need help, post the last working version and the new version with the error. Explain what you're trying to do in the new version.
    The error message indicates line 133. It looks like line 133 of your code is blank. Does your front end allow completely blank lines in the middle of a query? SQL*Plus doesn't by default; you have to say
    SET  SQLBLANKLINES  ONto have a completely blank line in SQL*Plus. (However, lines containing nothing but at commnet are always allowed.)
    You may have noticed that this site normally doesn't display multiple spaces in a row.
    Whenever you post formatted text (such as indented code) on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
    The 4 people who posted small code fragments for you to read all did this.  It would be so much easier for people to read your humongeous query if it were formatted.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How can i fetch records from 3 tables in a single query  without using join

    Hi.
    Can any body please tell me <b>How can i fetch records from 3 tables with a single query  without using joins</b>
    Thanx
    prabhudutta

    Hi Prabgudutta,
    We can fetch the data by using the views concept.
    Go throuth this info we can know the how to create view and same like database table only we can fetch the data.
    Views conatin the data at runtime only.
    Four different view types are supported. These differ in the
    way in which the view is implemented and in the methods
    permitted for accessing the view data.
    Database views are implemented with an equivalent view on
    the database.
    Projection views are used to hide fields of a table (only
    projection).
    Help views can be used as selection method in search helps.
    Maintenance views permit you to maintain the data
    distributed
    on several tables for one application object at one time.
    step by step creation of Maintenance view:
    With the help of the table maintenance generator, you are able to maintain the ENTRIES of the table in SM30 transaction.
    It can be set in transaction SE11 - Tools - Table maintenance generator.
    Table maintanance Generator is used to manually input values using transaction sm30
    follow below steps
    1) go to se11 check table maintanance check box under attributes tab
    2) utilities-table maintanance Generator-> create function group and assign it under
    function group input box. Also assign authorization group default &NC& .
    3) select standard recording routine radio in table table mainitainence generator to move table
    contents to quality and production by assigning it to request.
    4) select maintaience type as single step.
    5) maintainence screen as system generated numbers this dialog box appears when you click on create button
    6) save and activate table
    One step, two step in Table Maintenance Generator
    Single step: Only overview screen is created i.e. the Table Maintenance Program will have only one screen where you can add, delete or edit records.
    Two step: Two screens namely the overview screen and Single screen are created. The user can see the key fields in the first screen and can further go on to edit further details.
    SM30 is used for table maintenance(addition or deletion of records),
    For all the tables in SE11 for which Table maintenance is selected , they can be maintained in SM30
    Sm30 is used to maintain the table ,i.e to delete ,insert or modify the field values and all..
    It creates the maintenance screen for u for the aprticular table as the maintenance is not allowed for the table..
    In the SE11 delivery and maintenance tab, keep the maintenance allowed..
    Then come to the SM30 and then enter the table name and press maintain..,
    Give the authorization group if necessary and give the function group and then select maintenance type as one step and give the screen numbers as system specified..
    Then create,,,
    Then u will able to see the maintenance view for the table in which u can able to insert and delete the table values...
    We use SM30 transaction for entering values into any DB table.
    First we create a table in SE11 and create the table maintenance generator for that Table using (utilities-> table maintenance generator) and create it.
    Then it will create a View.
    After that from SM30, enter the table name and Maintain, create new entries, change the existing entries for that table.
    Hope this resolves your query.
    Reward all the helpful answers.
    Rgds,
    P.Naganjana Reddy

  • Variable for Query without changing Datamodel

    Hello Experts,
    I need in the Query a variable, which should have inpact on the processing in the user-exit. This variable should no be used directly for constraining data.
    It is an Yes/No variable. And I don't have in my multicube any characteristic which have Yes/No in their domain.
    In such situation I have always added to Multiprovider and Cube an new characteristic and created an variable for this characteristic.
    But I don't like this solution, because this changes in the datamodel are in really not important for the datamodel. It only an work around for getting the possibility of adding a variable.
    Does someone know an better idea for adding variables to query, without changing datamodel like this.
    Thanks
    Wojciech

    Hi Bhanu,
    Not really - I don't want to change Cubes.
    Thanks
    Wojciech

  • Tuning query without using hint

    Hi,
    i want to change the plan of a query without using hint.
    Also i want to use the plan that hint generate in my original query.
    My db is a 11g.
    How can i do this?
    tnx

    You can use SQL Plan Manager. You might find this interesting:
    http://blogs.oracle.com/optimizer/entry/what_should_i_do_with_old_hints_in_my_workload
    The link above basically suggests these steps:
    1. Run the query with the hints, then
    2. Take the plan from #1 and associate its SQL plan baseline with the query with no hints
    3. Remove the hints for that query in the code and start capturing and evolving plans for the un-hinted query

  • I want single update query without use the function.

    I want to update sells_table selling_code field with max date product_code from product table.
    In product table there is multiple product_code date wise.
    I have been done it with below quey with the use of function but can we do it in only one update query
    without use the function.
    UPDATE sells_table
    SET selling_code = MAXDATEPRODUCT(ctd_vpk_product_code)
    WHERE NVL(update_product_flag,0) = 0 ;
    CREATE OR REPLACE FUNCTION HVL.maxdateproduct (p_product IN VARCHAR2) RETURN NUMBER
    IS
    max_date_product VARCHAR2 (100);
    BEGIN
    BEGIN
    SELECT NVL (TRIM (product_code), 0)
    INTO max_date_product
    FROM (SELECT product_code, xref_end_dt
    FROM product
    WHERE TO_NUMBER (p_product) = pr.item_id
    ORDER BY xref_end_dt DESC)
    WHERE ROWNUM = 1; -- It will return only one row - max date product code
    EXCEPTION
    WHEN OTHERS
    THEN
    RETURN 0;
    END;
    RETURN max_date_product;
    END maxdateproduct;
    Thanks in Advance.

    Hi,
    Something like this.
    update setlls_table st
            set selling_code =(select nvl(trim(product_code)) from 
                                  (select product_code
                                          , rank() over (partition by item_id order by xref_end_dt DESC) rn
                                       from product
                                   ) pr
                                   where rn =1
                                         and pr.item_id = st.ctd_vpk_product_code
                               ) where NVL(update_product_flag,0) = 0 ;As such not tested due to lack of input sample.
    Regards
    Anurag Tibrewal.

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • Can u write the following query without using group by clause

    select sp.sid, p.pid, p.name from product p, supp_prod sp
    where sp.pid= p.pid and
    sp.sid = ( select sid from supp_prod group by sid
    having count(*) =(select count(*) from product));
    thru this, we retrieving all the products delivered by the supplier.
    Can you write the following query without using the group by clause

      select sp.sid, p.pid, p.name
        from product p, supp_prod sp
       where sp.pid= p.pid the above query will still retrieve all the products supplied by the supplier. sub-query is not necessary.
    maybe if you can post some sample data and output will help us understand what you want to achieve.

  • Looping a query with text fields

    I need to loop a query (fields: FName,LName,Phone) in
    textboxes.
    List all textboxes in one form and be able to update the db
    with all changes at once.
    <cfloop query="ListMembers">
    <input type="text" name="FName#MemberID#"
    value="#FName#">
    <input type="text" name="LName#MemberID#"
    value="#FName#">
    <input type="text" name="PhoneName#MemberID#"
    value="#FName#">
    </cfloop>
    <input type="Submit" value="Update">
    Now i need to update the db and not sure how to do that?
    Thanks for the help

    Yes. Two ways. One using Form Action and one using a form created by a form site and embedded into your web page: Embedding Forms & Encoding Email Addresses.
    OT

  • Execute query without navigation

    hi all,
    My problem today that I want to execute query without navigation.
    I create a global variable
    (:global.usr := :block.username;)
    as you see the global variable takes the value of the current user accessing the system.
    I want the user to be able to change his password without any privilage to see the other users.
    when_new_form_instance
    execute_query;
    that what I did but what I want is to restrict his access only according to the value in the global variable

    I tryed to run the following code but I dont know why the condition in the else clause always execute.     
    if :global.usr = :global.usr = upper('ADMIN') or
         :global.usr = upper('SYS_ADMIN') then
         set_block_property('SYS_USER', DEFAULT_WHERE, '');
         execute_query;
         else
                   set_block_property('SYS_USER', DEFAULT_WHERE, 'USER_NAME = :global.usr');
                   execute_query;
         end if;

Maybe you are looking for

  • Since I upgraded to Firefox 6.0.1, Facebook pages will not load properly.

    I am using a Mac 10.6.6. Just updated Firefox this week. When attempting to connect with Safari, I am not having any loading issues.

  • Asset Control Management

    Dear Colleagues, At the company where I work,Users for Asset control are using; KO01 - To create an investment order. ZAFE_0002 - To set up capital project (Customised Application) KO88 - for Settlement of investment order. AS01 - For creation of ass

  • How can I make a photo Christmas card in Mail?

    I want to send a Christmas card that includes a family photo. I've tried online services in the past, but they charge quite a bit for good cards. One place I like (good templates, easy to use) charges 3.99/mo or $48.00 a year. It's easy to import a p

  • How can i reload font information while the jvm is running.

    l.s. how can i reload font information while the jvm is running. I'm having the following problem... A program is running. A user installs a new font on the system (Xwindows in this case) Java doesn't display the font when i ask : java.awt.GraphicsEn

  • Exceptions & Conditions

    Hello Experts, Would anyone be able to help me with information on exceptions & conditions on a web template with multiple reports. We have 5 queries on web template, with all queries from the same multiprovider. We need exceptions and conditions but