Cursor fetch into

I have a function, to which i pass a sql statement.In the function using the cursor i get the output of the sql statemnt into a output variable. This function works fine, but for one particular select statement it takes[u] over an hour to execute in the FETCH...INTO statement specifically. The same select statement when executed outside in sql editor returns in under 2 mins.
The output of the sql is 5 records. Any ideas on why it is taking forever in the FETCH...INTO statment?
FUNCTION getOutput(p_sql VARCHAR2)
RETURN VARCHAR2
AS
TYPE extractlinecurtype IS REF CURSOR;
cur_extract extractlinecurtype;
v_oneextractline VARCHAR2 (32767);
v_output VARCHAR2 (32767) := NULL;
crlf CHAR (2) := CHR (10) || CHR (13);
BEGIN
OPEN cur_extract FOR p_sql;
dbms_output.put_line(sysdate);
LOOP
FETCH cur_extract
INTO v_oneextractline;
EXIT WHEN cur_extract%NOTFOUND;
v_output := v_output || CHR (10) || v_oneextractline;
END LOOP;
IF cur_extract%ISOPEN
THEN
CLOSE cur_extract;
END IF;
v_output := v_output || crlf || crlf;
return v_output;
EXCEPTION
WHEN OTHERS
THEN
dbms_output.put_line('error in getoutput' || SQLCODE || SQLERRM);
RETURN NULL;
END getOutput;

I cannot send the sql cause it uses our internal tables. My question is why does a query take under 2mins to run in sql editor and over an hr in pl/sql with cursor?
Is there something wrong in declaration?

Similar Messages

  • Dynamic query and Open Cursor fetch into ??

    Hi;
    I think we took the wrong turn some were... because looks like a dead end...
    What I need to do:
    Create a procedure to compare two generic tables in diferente databases and find rows that have diferent data.
    (No restritions on what tables it might compare)
    What I have done:
    CREATE OR REPLACE PROCEDURE COMPARE_TABLES_CONTENT_V3(
    tableName_A IN VARCHAR2, tableName_B IN VARCHAR2, filter IN VARCHAR2) AS
    -- Get all collumns present in [TableName_A]
    -- This is necessary to create a dynamic query
    select column_name bulk collect
    into v_cols1
    from all_tab_columns
    where table_name = v_tab1
    and owner = nvl(v_own1, user)
    order by column_id;
    -- If there is no columns in table A ... them no need to proceed
    if v_cols1.count = 0 then
    dbms_output.put_line('[Error] reading table ' || tableName_A ||
    ' collumns information. Exit...');
    return;
    end if;
    -- Collect Collumns information by ',' separated, for the query
    for i in 1 .. v_cols1.count loop
    compareCollumns := compareCollumns || ',' || v_cols1(i);
    end loop;
    -- Create the query that gives the diferences....
    getDiffQuery := 'select ' || compareCollumns ||
    ' , count(src1) CNT1, count(src2) CNT2 from (select a.*, 1 src1, to_number(null) src2 FROM ' ||
    tableName_A ||
    ' a union all select b.*, to_number(null) src1, 2 src2 from ' ||
    tableName_B || ' b) group by ' || compareCollumns ||
    ' having count(src1) <> count(src2)';
    Whats the problem?
    I'm lost...I can access the query using debugging on the oracle procedure... and run it OK on a new SQL window. No problem here... its perfect.
    But I dont know how to access this information inside the procedure, I need to format the information a bit...
    I try a cursor...
    open vCursor for getDiffQuery;
    fetch vCursor into ??????? -- Into what... query, columns and tables is all dynamic...
    close vCursor;
    Any ideas..

    Making the issue more simple....
    At this point I have a oracle procedure that generates a dynamic sql query, based on the table names passed by parameter.
    getDiffQuery := 'select ' || compareCollumns || (.....)
    end procedure;
    This ''getDiffQuery'' contains a query that gets the diferences in the tables. (Working fine)
    I would like to access this information in the same procedure.
    I cant use cursor because the table, columns... actualy all is dynamic based on the generated query:( !

  • Cursor Fetch into Record vs Record Components

    Anyone,
    What are pros and cons of the use of the cursor method that fatches into a single record value vs. that of record components?
    I am just not seeing why you might use one vs other so was curious if their is something I am missing in reagrds to performance or temp space required or anything like that????
    Thanks for any help,
    Miller

    You should use record components when possible. It is easier to code and read.
    Cursor loops are great:
    for c_rec in select a, b, c from t1, t2 where t1.a = t2.a loop
    dbms_output.put_line(c_rec.b || c_rec.a);
    end loop;
    In the above case you don't even have to declare c_rec!
    Tom Best

  • Error when fetching into a variable of ROWTYPE

    I came across with a vied error when creating the body of a method.
    Problem:
    I have a Type called Emp_typ and a corresponding table Emp_tab.
    When I create the body of get method it gives the following error msg :
    Compilation errors for PACKAGE BODY CHAMITH.EMP_API
    Error: PLS-00386: type mismatch found at 'EMP_ROW' between FETCH cursor and INTO variables
    Line: 9
    Text: fetch get_emp_rec into emp_row;
    Get Method body :
    function get(id_ number) return emp_tab%rowtype IS
    cursor get_emp_rec IS
    select * from emp_tab;
    emp_row emp_tab%rowtype;
    begin
    open get_emp_rec;
    fetch get_emp_rec into emp_row;
    close get_emp_rec;
    return emp_row;
    end;
    However when I do the same without having a type upon the table emp_tab, it works fine.
    Pls give me a solution for that.
    Thanks,
    Chamith

    Hi,
    Let me know the Oracle Database Version.
    Regards,
    Sailaja

  • PLS-00386 when fetching into a previously declared type.

    I received error PLS-00386 when trying to fetch a cursor into a variable based on an object:
    SQL >-- Create type based on scott.emp
    SQL >CREATE OR REPLACE TYPE t_emp AS OBJECT
    2 (
    3 empno NUMBER(4),
    4 ename VARCHAR2(10),
    5 job VARCHAR2(9),
    6 mgr NUMBER(4),
    7 hiredate DATE,
    8 sal NUMBER(7, 2),
    9 comm NUMBER(7, 2),
    10 deptno NUMBER(2)
    11 );
    12 /
    Type created.
    SQL >
    SQL >show error
    No errors.
    SQL >
    SQL >-- Create a function that fetches records into t_emp:
    SQL >
    SQL >CREATE OR REPLACE FUNCTION emp_fn RETURN NUMBER IS
    2 l_emp t_emp;
    3 CURSOR c1 IS
    4 SELECT * FROM emp;
    5 BEGIN
    6 OPEN c1;
    7 LOOP
    8 FETCH c1
    9 INTO l_emp;
    10 EXIT WHEN c1%NOTFOUND;
    11 END LOOP;
    12 RETURN 0;
    13 END;
    14 /
    Warning: Function created with compilation errors.
    SQL >
    SQL >show error
    Errors for FUNCTION EMP_FN:
    LINE/COL ERROR
    8/5 PL/SQL: SQL Statement ignored
    9/12 PLS-00386: type mismatch found at 'L_EMP' between FETCH cursor
    and INTO variables
    SQL >
    Now when I declare the type exactly the same way inside the function, the function compiles and executes correctly:
    SQL >@test_emp2
    SQL >CREATE OR REPLACE FUNCTION emp_fn RETURN NUMBER IS
    2
    3 TYPE t_emp_rec IS RECORD(
    4 empno NUMBER(4)
    5 ,ename VARCHAR2(10)
    6 ,job VARCHAR2(9)
    7 ,mgr NUMBER(4)
    8 ,hiredate DATE
    9 ,sal NUMBER(7, 2)
    10 ,comm NUMBER(7, 2)
    11 ,deptno NUMBER(2));
    12
    13 l_emp t_emp_rec;
    14
    15 CURSOR c1 IS
    16 SELECT * FROM emp;
    17 BEGIN
    18 OPEN c1;
    19 LOOP
    20 FETCH c1
    21 INTO l_emp;
    22 EXIT WHEN c1%NOTFOUND;
    23 dbms_output.put_line( l_emp.empno);
    24 END LOOP;
    25 RETURN 0;
    26 END;
    27 /
    Function created.
    SQL >
    SQL >show error
    No errors.
    SQL >
    SQL >select emp_fn from dual;
    EMP_FN
    0
    1 row selected.
    Why can does the first function not compile and return PLS-00386?
    Thanks,
    Christoph

    Hi, Christoph,
    Christoph wrote:
    ... BTW: What do you do to format the code nicely like you did?This site normally doesn't display multiple spaces in a row.
    Whenever you post formatted text (such as query results) on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to fetch into any type of record

    Hi,
    I have a problem. I have to create a procedure to which any query is given it is able to execute and then fetch into the record create based on the cursor. here is the code
    procedure execute_query(p_sql varchar2) is
    type t_rc is ref cursor;
    l_rc t_rc;
    v_t_record l_rc%rowtype;
    begin
    --dbms_lock.sleep(10);
    open l_rc for p_sql;
    loop
    fetch l_rc
    into v_t_record;
    dbms_output.put_line(v_t_record.object_name);
    exit when l_rc%notfound;
    end loop;
    v_query_row_count := l_rc%rowcount;
    -- dbms_output.put_line(v_query_row_count);
    end execute_query;
    constraints:
    i can specify return clause in ref cursor.
    I have to fetch into the records from different queries
    thanks
    Regards
    nick
    Edited by: Nick Naughty on Dec 21, 2008 5:16 AM

    Yes, as I already mentioned, you could use DBMS.SQL:
    create or replace
      procedure p1(
                   p_query varchar2
      is
          c           number;
          d           number;
          col_cnt     integer;
          f           boolean;
          rec_tab     dbms_sql.desc_tab;
          v_number    number;
          v_string    varchar2(4000);
          v_date      date;
          v_rownum    number;
      begin
          c := dbms_sql.open_cursor;
          dbms_sql.parse(c,p_query, dbms_sql.native);
          dbms_sql.describe_columns(c,col_cnt,rec_tab);
          for col_num in 1..rec_tab.count loop
            if rec_tab(col_num).col_type = 1
              then
                dbms_sql.define_column(c,col_num,v_string,rec_tab(col_num).col_max_len);
            elsif rec_tab(col_num).col_type = 2
              then
                dbms_sql.define_column(c,col_num,v_number);
            elsif rec_tab(col_num).col_type = 12
              then
                dbms_sql.define_column(c,col_num,v_date);
              else raise_application_error(-20900,'unsupported data type');
            end if;
          end loop;
          d := dbms_sql.execute(c);
          v_rownum := 0;
          loop
            exit when dbms_sql.fetch_rows(c) = 0;
            v_rownum := v_rownum + 1;
            dbms_output.put_line('row ' || v_rownum);
            for col_num in 1..rec_tab.count loop
              if rec_tab(col_num).col_type = 1
                then
                  dbms_sql.column_value(c,col_num,v_string);
                  dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_string);
              elsif rec_tab(col_num).col_type = 2
                then
                  dbms_sql.column_value(c,col_num,v_number);
                  dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_number);
              elsif rec_tab(col_num).col_type = 12
                then
                  dbms_sql.column_value(c,col_num,v_date);
                  dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_date);
                else
                  raise_application_error(-20900,'unsupported data type');
              end if;
            end loop;
          end loop;
          dbms_sql.close_cursor(c);
        exception
          when others
            then
              if dbms_sql.is_open(c)
                then
                  dbms_sql.close_cursor(c);
              end if;
              raise;
    end;
    set serveroutput on format wrapped
    exec p1('select ename,sal,hiredate from emp');
    SQL> create or replace
      2    procedure p1(
      3                 p_query varchar2
      4                )
      5    is
      6        c           number;
      7        d           number;
      8        col_cnt     integer;
      9        f           boolean;
    10        rec_tab     dbms_sql.desc_tab;
    11        v_number    number;
    12        v_string    varchar2(4000);
    13        v_date      date;
    14        v_rownum    number;
    15    begin
    16        c := dbms_sql.open_cursor;
    17        dbms_sql.parse(c,p_query, dbms_sql.native);
    18        dbms_sql.describe_columns(c,col_cnt,rec_tab);
    19        for col_num in 1..rec_tab.count loop
    20          if rec_tab(col_num).col_type = 1
    21            then
    22              dbms_sql.define_column(c,col_num,v_string,rec_tab(col_num).col_max_len);
    23          elsif rec_tab(col_num).col_type = 2
    24            then
    25              dbms_sql.define_column(c,col_num,v_number);
    26          elsif rec_tab(col_num).col_type = 12
    27            then
    28              dbms_sql.define_column(c,col_num,v_date);
    29            else raise_application_error(-20900,'unsupported data type');
    30          end if;
    31        end loop;
    32        d := dbms_sql.execute(c);
    33        v_rownum := 0;
    34        loop
    35          exit when dbms_sql.fetch_rows(c) = 0;
    36          v_rownum := v_rownum + 1;
    37          dbms_output.put_line('row ' || v_rownum);
    38          for col_num in 1..rec_tab.count loop
    39            if rec_tab(col_num).col_type = 1
    40              then
    41                dbms_sql.column_value(c,col_num,v_string);
    42                dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_string);
    43            elsif rec_tab(col_num).col_type = 2
    44              then
    45                dbms_sql.column_value(c,col_num,v_number);
    46                dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_number);
    47            elsif rec_tab(col_num).col_type = 12
    48              then
    49                dbms_sql.column_value(c,col_num,v_date);
    50                dbms_output.put_line('  ' || rec_tab(col_num).col_name || ' = ' || v_date);
    51              else
    52                raise_application_error(-20900,'unsupported data type');
    53            end if;
    54          end loop;
    55        end loop;
    56        dbms_sql.close_cursor(c);
    57      exception
    58        when others
    59          then
    60            if dbms_sql.is_open(c)
    61              then
    62                dbms_sql.close_cursor(c);
    63            end if;
    64            raise;
    65  end;
    66  /
    Procedure created.
    SQL> set serveroutput on format wrapped
    SQL> exec p1('select ename,sal,hiredate from emp');
    row 1
      ENAME = SMITH
      SAL = 800
      HIREDATE = 17-DEC-80
    row 2
      ENAME = ALLEN
      SAL = 1600
      HIREDATE = 20-FEB-81
    row 3
      ENAME = WARD
      SAL = 1250
      HIREDATE = 22-FEB-81
    row 4
      ENAME = JONES
      SAL = 2975
      HIREDATE = 02-APR-81
    row 5
      ENAME = MARTIN
      SAL = 1250
      HIREDATE = 28-SEP-81
    row 6
      ENAME = BLAKE
      SAL = 2850
      HIREDATE = 01-MAY-81
    row 7
      ENAME = CLARK
      SAL = 2450
      HIREDATE = 09-JUN-81
    row 8
      ENAME = SCOTT
      SAL = 3000
      HIREDATE = 19-APR-87
    row 9
      ENAME = KING
      SAL = 5000
      HIREDATE = 17-NOV-81
    row 10
      ENAME = TURNER
      SAL = 1500
      HIREDATE = 08-SEP-81
    row 11
      ENAME = ADAMS
      SAL = 1100
      HIREDATE = 23-MAY-87
    row 12
      ENAME = JAMES
      SAL = 950
      HIREDATE = 03-DEC-81
    row 13
      ENAME = FORD
      SAL = 3000
      HIREDATE = 03-DEC-81
    row 14
      ENAME = MILLER
      SAL = 1300
      HIREDATE = 23-JAN-82
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Is it possible to parse a cursor record into a function if so how?

    My question is basically as the title says. I would like to know if i can parse a cursor record through a function
    for example
    FOR r1 in c1
    LOOP
    calcuate(r1.mark1, r1.mark2,r1.mark3, r1.student)
    END LOOP
    where fucntion looks similar to
    CREATE function calculate(mark1 NUMBER, mark2 NUMBER, mark3 NUMBER, student VARCHAR2(15))
    RETURN NUMBER
    IS
    total NUMBER;
    BEGIN
    SELECT avg(all_marks) into total
    from table
    where mark1 = table.mark10 and
    mark2 > 100 and
    mark3 > 70;
    return NVL(total,0);
    end;
    if this is not possible how else can i do the specific task, where a cursor record is used to do a similar query to what the function does.

    > yes my system needs to be very efficient and with the least amount of code, besides this is for a
    database programming assingment where pl/sql is the language we are meant to master thus pl/sql >
    sql sad to say.
    Sorry, I don't get it. You say that your system needs to be efficient and in the very same breath you say "Screw efficiency, I want to use PL/SQL and not SQL". That is a bs statement IMO.
    There is a correct way. There is a wrong way. Choose one. It is not possible to do it correctly using the wrong principles, approach and code.
    > can cursor fetch loop parse through a function or am i just dreaming for that shortcut??
    What shortcut? A cursor is a SQL program. You are using PL/SQL to fetch from that cursor, iterating through it, does not change the fact that you are
    a) dealing with a SQL program (aka cursor)
    b) being serviced by the SQL engine
    What shortcut do you imagine there is? Not dealing with the SQL engine at all? PL/SQL is two languages. It is PL, a Programming Language based on ADA. This is tightly integrated with the SQL language.
    PL code is parsed and executed by the PL engine. SQL code is parsed and executed by the SQL engine. Each line of SQL code in PL/SQL, requires a context switch from the PL engine to the SQL engine in order for that SQL code to be executed. Each fetch from a cursor (via an explicit or implicit fetch), is a context switch to the SQL engine to execute that cursor in order to get the next set of (or single) rows.
    The more context switches there are, the more overheads there are. Each context switch pulls and/or pushes data between the PL and SQL engines.
    Okay, so now where is the "shortcut" - or more accurately called, correctly designing and coding PL/SQL?
    This is achieved by not pulling data into the PL engine that you do not really need as your PL code only pushes that very same data back to the SQL engine.
    This is achieved by reducing the number of context switches between the PL and SQL engines.
    This is achieved by processing data sets and not rows, in the PL engine.
    This is achieved by Maximizing SQL and minimizing PL/SQL.
    Now if you do not like and want to play silly buggers with PL/SQL and Oracle, that's your decision. I'm sure some here will throw you a rope (by posting really smelly code) from the bottom of the Oracle scalability and performance hole and help you drag yourself into it.

  • Cursor fetch with multiple rows

    I have been given code which, effectively, looks like this:
    declare
      cursor cTest is select * from (select 'one' from dual union all select 'two' from dual);
      lvText varchar2(10);
    begin
      open cTest;
      fetch cTest into lvText;
      close cTest;
      dbms_output.put_line(lvText);
    end;
    /That consistently returns just the result "one", even though there are two rows in the cursor.
    If I re-write the code like this:
    declare
      lvText varchar2(10);
    begin
      select thecol into lvText from (select 'one' as thecol from dual union all select 'two' from dual);
      dbms_output.put_line(lvText);
    end;
    /...then I get the error I expected (ORA-01422: exact fetch returns more than requested number of rows).
    I guess I am a bit surprised the "fetch into" syntax (which I haven't used before) works at all. Why doesn't it spot that multiple rows are being loaded into a single variable and explode? I am guessing that which row 'wins' the competition to be assigned to the variable is completely indeterminate? Any doco on this behaviour you can point me at, please? The bit I read (http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/fetch_statement.htm) simply says that "You must use either a cursor FOR loop or the FETCH statement to process a multiple-row query", but it doesn't say "the code will silent grab one of the rows at random if you use the FETCH statement on a multiple-row resultset".
    (tested on 11.2.0.3, if it makes a difference)

    >
    I realise FETCH fetches one row. The problem is that without a loop, doing so is 'wrong', logically. But it's still allowed, and I can't find anywhere in the doco that says "if you use FETCH, you MUST loop through the results, otherwise we will only return the first row, which will then probably be a bug in your code".
    I'm asking for something from the doco (or from a website somewhere, maybe) that explains that "fetch without loops will work, but will be wrong". It's not something I expected, put it that way. It's something I would have thought others would have tripped over before now, anyway. In what I've read, like your code example here, you just see fetch-with-loops, with the unstated assumption made that you'd never do it any other way. I've not seen it explicitly said that it's possible to do it without a loop, but would be a dumb thing to do.
    >
    Oracle has no way to know what a developer is trying to do or how many rows a cursor will return.
    It isn't necessarily true that "fetch without loops will work, but will be wrong". My cursor might only return one row.
    What about a BULK COLLECT INTO without a LIMIT clause? Should Oracle tell you that if you query too many records you may run out of memory?
    What about a BULK COLLECT INTO with a limit clause? Should Oracle tell you that "bulk collect with limit without a loop will work, but will be wrong"? It isn't necessarily wrong either.

  • Cursor Fetch Loop to Move data from one table to another

    Hi Gurus,
    I need to write a cursor fetch loop PL/SQL procedure that moves all the data from the students table to student_history table. Can anyone tell me how to do this please?
    Thanks

    and the instructions were specific:"Write a PL/SQL procedure that moves all the data from the students table to student_history table. You should use CURSOR FETCH LOOP to retrieve the data"
    Who in the world have given you that instruction? This person[s] should take their ABC learning too. I might have come up with 1001 ineffective and owful ways to perform some task, but to teach others to do it in the same way would be a bit arrogant. To copy the content of a table into another one in CURSOR FETCH LOOP is one of the worst solutions I've heard of. See
    http://asktom.oracle.com/pls/ask/f?p=4950:8:16840616406862738180::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:5008574230335

  • My cursor turns into a revolving rainbow disk. How do I repair this problem?

    My cursor turns into a revolving rainbow disk and remains frozen in that state. The only way I can "unfreeze" the disk is to turn off the computer and reboot. What is causing the problem and how do I fix it without rebooting?

    Thomas,
    What you are referring to is called the Spinning Beach Ball of Death and can be caused by many things. One of the most prominent is a lack of RAM. Keep Activity Monitor open (Applications - Utilities - Activity Monitor) and the next time you have a SBBOD take a look at AM and see how much Free RAM you have. You will find this information under the System Memory tab. As a general rule of thumb if it's less than 500MB you probably need a RAM upgrade.
    You can also read and apply information from the Spinning Beach Ball of Death article.
    Also did you know you were posting in an iMac forum?
    Roger

  • Diference bte fetch into statement fetch bulkcollect into statement.

    hi,,
    difference btw fetch into statement and bulk collect into statement.
    differece btw for loop and forall loop.

    Hi,
    Syntax:
    (Select)(Fetch)(execute immediate) … BULK COLLECT Into collection_name [,collection_name, …] [LIMIT max_lines] ;
    LIMIT is used to limit the number of rows returned.
    BULK COLLECT can also be used to retrieve the result of a DML statement that uses the RETURNING INTO clause:
    SQL> Declare
    2 TYPE TYP_TAB_EMPNO IS TABLE OF EMP.EMPNO%Type ;
    3 TYPE TYP_TAB_NOM IS TABLE OF EMP.ENAME%Type ;
    4 Temp_no TYP_TAB_EMPNO ;
    5 Tnoms TYP_TAB_NOM ;
    6 Begin
    7 -- Delete rows and return the result into the collection --
    8 Delete From EMP where sal > 3000
    9 RETURNING empno, ename BULK COLLECT INTO Temp_no, Tnoms ;
    10 For i in Temp_no.first..Temp_no.last Loop
    11 dbms_output.put_line( 'Fired employee : ' || To_char( Temp_no(i) ) || ' ' || Tnoms(i) ) ;
    12 End loop ;
    13 End ;
    14 /
    Fired employee : 7839 KING
    try to understand this example.i think it is sufficient.
    Thanks,
    Sanjeev.

  • Cursors turn into cross-hairs sometimes.

    I have Adobe Photoshop CS4. My pen tool and paint brush tool cursors turn into hard-to-see cross-hairs sometimes. It makes my work more difficult and strains my eyes and give me a headache. How does it do that in the first place and how do I fix it?

    THANK YOU THANK YOU THANK YOU THANK YOU
    I had no idea what was doing it! Give yourself a cookie or something for me!

  • Cursor turned into a cross

    My cursor turned into a plus sign and when I try to select something in it it spazzes. I'm having trouble working with it.

    Do SMC and PRAM reset.
    Intel-based Macs: Resetting the System Management Controller (SMC) - Apple Support
    OS X Mavericks: Reset your computer’s PRAM

  • Need a test for fetch into from a cursor or an alternative.

    I've got a small oddity.
    Ultimately it's two cursors that are exactly the same except a part of the WHERE clause. Depending on how many records I am going through (based on a counter) I need to use one cursor or the other as the outer loop, to then process an inner loop.
    My problem is I don't quite know how to exit out of the current loop when the fetch reaches the end of the cursor. Meaning, in each of these loops, there's an IF THEN to see which cursor I'm using, and then fetch from the correct one. If I use a FOR X IN CURSOR loop, I'll have to double up the code, one for each outer cursor loop.
    What's the test in Oracle for no record found or when it hits the end of a cursor with a fetch? Or am I stuck and I can only use the exception block when the fetch fails?
    Thansk!

    You can check whether your fetch returned any data with %notfound.
    DECLARE
      l_cur   sys_refcursor;
      l_var   VARCHAR2 (30);
    BEGIN
      IF TO_CHAR (SYSDATE, 'Day') = 'Monday'
      THEN
        OPEN l_cur FOR
          SELECT dummy
            FROM DUAL;
      ELSE
        OPEN l_cur FOR
          SELECT dummy
            FROM DUAL
           WHERE 1 = 2;
      END IF;
      LOOP
        FETCH l_cur
         INTO l_var;
        EXIT WHEN l_cur%NOTFOUND;
        DBMS_OUTPUT.put_line (l_var);
      END LOOP;
    END;
    /

  • Loop Cursor Fetch Insert Rows Into Global Temporay Table

    Hi friends,
    I want to know there where is the problem in my this query
    cursor c1 is select empno
    from scott.emp;
    b number;
    begin
    open c1;
         loop
         fetch c1 into b;
         exit when c1%notfound;
    insert into scheema.a1
    (a)
    values
    (b);
    commit;
         end loop;
         close c1;
    end;
    There is no any error displaying while proceeding this query but the table a1 which is (Global temporary table with ON COMMIT PRESERVE ROWS option enabled) does not gets any records. Kindly let me know the problem.
    Regards.

    Are you sure that the query returns date? Are you checking the table via the same session?
    SQL> create global temporary table a1 (
      a  number)
      on commit preserve rows
    Table created.
    SQL> declare
       cursor c1 is select empno
          from scott.emp;
       b number;
    begin
       open c1;
       loop
          fetch c1 into b;
          exit when c1%notfound;
          insert into a1
          (a)
          values
          (b);
          commit;
       end loop;
       close c1;
    end;
    PL/SQL procedure successfully completed.
    SQL> select * from a1
             A
          7369
          7499
          7521
          7566
          7654
          7698
          7782
          7788
          7839
          7844
          7876
          7900
          7902
          7934
    14 rows selected.

Maybe you are looking for