2x cursor: 1x user_tables + 1 x tab_cols

Hi guys,
I was just wondering whether it is possible to dynamicaly get all user tables then all user columns and write all content to a file.
What I am trying to do is export all the tables listed in user_tables.
I have got the loop already for the user_tables and the loop for the user_columns too,
but then I need to get the column name dynamically and then something like this doesn't work:
OPEN the_cursor_table FOR 'select * from '|| utables.table_name;
LOOP
table_name := utables.table_name;
* FETCH the_cursor_table INTO table_name%rowtype;
EXIT WHEN the_cursor_table%NOTFOUND;
It is not working on the line with an astrix, how can this be fixed?
4Eyes

cant you generate a script like this. if you want you can make changes and add extra commas so that you can spool it to CSV.
SQL> WITH my_user_tables AS
  2       ( SELECT 'test1' AS tab_name, 'user1' AS user_name
  3          FROM DUAL
  4        UNION ALL
  5        SELECT 'test2' AS tab_name, 'user1' AS user_name
  6          FROM DUAL
  7        UNION ALL
  8        SELECT 'test3' AS tab_name, 'user1' AS user_name
  9          FROM DUAL )
10  --
11     , my_all_tab_columns AS
12       ( SELECT 'num1' AS col_name, 'test1' AS tab_name
13          FROM DUAL
14        UNION ALL
15        SELECT 'sno1' AS col_name, 'test1' AS tab_name
16          FROM DUAL
17        UNION ALL
18        SELECT 'dno1' AS col_name, 'test1' AS tab_name
19          FROM DUAL
20        UNION ALL
21        SELECT 'num2' AS col_name, 'test2' AS tab_name
22          FROM DUAL
23        UNION ALL
24        SELECT 'sno2' AS col_name, 'test2' AS tab_name
25          FROM DUAL
26        UNION ALL
27        SELECT 'dno2' AS col_name, 'test2' AS tab_name
28          FROM DUAL
29        UNION ALL
30        SELECT 'num3' AS col_name, 'test3' AS tab_name
31          FROM DUAL
32        UNION ALL
33        SELECT 'sno3' AS col_name, 'test3' AS tab_name
34          FROM DUAL
35        UNION ALL
36        SELECT 'dno3' AS col_name, 'test3' AS tab_name
37          FROM DUAL )
38  --
39  SELECT col_name, tab_name
40    FROM my_all_tab_columns
41   WHERE tab_name IN ( SELECT tab_name
42                          FROM my_user_tables
43                         WHERE user_name = 'user1' );
COL_ TAB_N
num1 test1
sno1 test1
dno1 test1
num2 test2
sno2 test2
dno2 test2
num3 test3
sno3 test3
dno3 test3
9 rows selected.
SQL> WITH my_user_tables AS
  2       ( SELECT 'test1' AS tab_name, 'user1' AS user_name
  3          FROM DUAL
  4        UNION ALL
  5        SELECT 'test2' AS tab_name, 'user1' AS user_name
  6          FROM DUAL
  7        UNION ALL
  8        SELECT 'test3' AS tab_name, 'user1' AS user_name
  9          FROM DUAL )
10  --
11     , my_all_tab_columns AS
12       ( SELECT 'num1' AS col_name, 'test1' AS tab_name
13          FROM DUAL
14        UNION ALL
15        SELECT 'sno1' AS col_name, 'test1' AS tab_name
16          FROM DUAL
17        UNION ALL
18        SELECT 'dno1' AS col_name, 'test1' AS tab_name
19          FROM DUAL
20        UNION ALL
21        SELECT 'num2' AS col_name, 'test2' AS tab_name
22          FROM DUAL
23        UNION ALL
24        SELECT 'sno2' AS col_name, 'test2' AS tab_name
25          FROM DUAL
26        UNION ALL
27        SELECT 'dno2' AS col_name, 'test2' AS tab_name
28          FROM DUAL
29        UNION ALL
30        SELECT 'num3' AS col_name, 'test3' AS tab_name
31          FROM DUAL
32        UNION ALL
33        SELECT 'sno3' AS col_name, 'test3' AS tab_name
34          FROM DUAL
35        UNION ALL
36        SELECT 'dno3' AS col_name, 'test3' AS tab_name
37          FROM DUAL )
38  --
39  SELECT        'select '
40             || SUBSTR ( MAX ( SYS_CONNECT_BY_PATH ( col_name, ',' )), 2 )
41             || ' from '
42             || tab_name
43             || ';' query
44        FROM ( SELECT tab_name, col_name
45                    , ROW_NUMBER ( ) OVER ( PARTITION BY tab_name ORDER BY col_name )
46                           rn
47                FROM my_all_tab_columns
48               WHERE tab_name IN ( SELECT tab_name
49                                       FROM my_user_tables
50                                      WHERE user_name = 'user1' ))
51  START WITH rn = 1
52  CONNECT BY PRIOR rn = rn - 1 AND PRIOR tab_name = tab_name
53    GROUP BY tab_name
54    ORDER BY tab_name;
QUERY
select dno1,num1,sno1 from test1;
select dno2,num2,sno2 from test2;
select dno3,num3,sno3 from test3;
SQL>

Similar Messages

  • How can i find total no of records in a cursor

    Hi
    Its very helpful if anybody reply my doubt
    I have a cursor declared in a procedure.
    i opened that cursor, i want to know the count of records in the opened cursor, means total count of records.
    is it possible??
    Pls reply ASAP

    HI,
    this is a simple example with a cursor over user_tables view:
    set serveroutput on
    DECLARE
       CURSOR c_test IS
          SELECT table_name FROM user_tables;
       type t_reg is table of varchar2(30);
       l_reg t_reg;
    BEGIN
       OPEN c_test;
       fetch c_test bulk collect into l_reg;
       dbms_output.put_line(c_test%ROWCOUNT);
    END;
    /Output:
    SQL>
    SQL> set serveroutput on
    SQL> DECLARE
      2     CURSOR c_test IS
      3        SELECT table_name FROM user_tables;
      4     type t_reg is table of varchar2(30);
      5     l_reg t_reg;
      6  BEGIN
      7     OPEN c_test;
      8 
      9     fetch c_test bulk collect into l_reg;
    10 
    11     dbms_output.put_line(c_test%ROWCOUNT);
    12  END;
    13  /
    42
    PL/SQL procedure successfully completed
    SQL> Regards,
    Edited by: Walter Fernández on Nov 29, 2008 8:59 AM - Adding output...

  • How can i find total no of files in a directory ..using fileconnection

    how can i find total no of files in a directory.
    currently iam using
    Enumeration enum = fileconn.list(); and by iterating iam getting count.
    but the application is getting stuckup when the number of files in the directory is large.
    Is there any alternative. please help.
    thanks in advance.

    HI,
    this is a simple example with a cursor over user_tables view:
    set serveroutput on
    DECLARE
       CURSOR c_test IS
          SELECT table_name FROM user_tables;
       type t_reg is table of varchar2(30);
       l_reg t_reg;
    BEGIN
       OPEN c_test;
       fetch c_test bulk collect into l_reg;
       dbms_output.put_line(c_test%ROWCOUNT);
    END;
    /Output:
    SQL>
    SQL> set serveroutput on
    SQL> DECLARE
      2     CURSOR c_test IS
      3        SELECT table_name FROM user_tables;
      4     type t_reg is table of varchar2(30);
      5     l_reg t_reg;
      6  BEGIN
      7     OPEN c_test;
      8 
      9     fetch c_test bulk collect into l_reg;
    10 
    11     dbms_output.put_line(c_test%ROWCOUNT);
    12  END;
    13  /
    42
    PL/SQL procedure successfully completed
    SQL> Regards,
    Edited by: Walter Fernández on Nov 29, 2008 8:59 AM - Adding output...

  • Help plss Should a cursor be created in database or program unit in forms l

    Where can i create a cursor should it be in database i.e isql plus using internet explorer or should i create the cursor in forms
    forms can i create the cursor so that i can use the cursor in the post item trigger of the userid item of login page
    Can i create the cursor in a package specification and how can i call that cursor into a post_text item trigger
    details below
    Iam working on a Online exam for students Dummy project using forms
    The student 1st comes to a WELCOME page i.e form which asks him if he is a new student or registered stud
    If he clicks registered pushbutton it takes him to a LOGIN page where there are two items userid and password.I am using a Validate_item OR Post_item trigger to fire after the student enters his userid.
    I need to compare the entered userid with the userid stored in the database STUDENTS table which i created which consists of the registered students details.
    Our project Guide suggested that we create a cursor and fetch each userid everytime into the cursor and compare the entered userid with the cursor.

    hi Sqlstar,
    Regarding to your all questions , i prefered that you search for a simple document or book in order to understand much more more about creating cursors.
    however since this forum made to help people and to make a integerated community i would be glad to offer my help and going with you step bt step with your answers.
    as for beginning
    1) when you create the user_exists function in database , if you want to call it in the form level you would wirte a code like this
    ----WHEN-VALIDATE-ITEM------------user_id item---------
    IF scheme_name.procedure_name(:block1.userid)!='TURE' then
       message('User Name is not correct');
       raise form_trigger_failure;
    end if;
    2) it's not a waste of recourse to make 2 validation of both userid and password
    and as same you create a procedure for useris you should create a procedure for password where the user id is the outcomming of userid item in the form.
    3)you have all choices to create the 2 procedures on database side or in the program unit at design time for the form it's up to you and as a small hint for you ,
    when you create them in the program unit and want to call the procedures in the WHEN-VALIDATE-ITEM  trigger you should denote the procedure name with the name of package first as following
    -------[Package Spec Code]-------
    PACKAGE user_validation IS
    function user_exists (p_user_id number) return boolean;
    end ;
    --------------[Package Body Code]------
    PACKAGE BODY user_validation IS
    function user_exists (p_user_id number) return boolean
    is
    user_number number;
    cursor c_user_id is
    select user_id from <user_table>
    where user_id=p_user_id;
    begin
    open c_user_id ;
    loop
    fetch user_id  into user_number ;
    return (true);
    exit when c_user_id%NOTFOUND;
    end loop;
    execption  when others then
    return(false);
    end user_exists;
    end;
    -----Calling at Validation level-----------
    IF (user_validation.user_exists(:block1.userid))!=TURE then
       message('User Name is not correct');
       raise form_trigger_failure;
    end if;
    i hope this could be helpful engouh to start your application
    Good Luck .
    Regards
    Omar                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Doubt in Cursor

    Hi,
    I want to populate a table with table name and total number of rows for all tables in the database.
    code is:
    1 declare
    2 cursor c1 is select table_name from user_tables;
    3 l_count number;
    4 begin
    5 for mvr in c1
    6 loop
    7 select count(*) into l_count where table=mvr.table_name;
    8 insert into x values(mvr.table_name,l_count);
    9 end loop;
    10 end;
    The pbm is on line 7...mvr.table_name should be declared...
    Pls guide me..
    Adios..

    Another solution with pipelined function (since 9i database)
    CREATE TYPE TYP_TB_COUNT AS OBJECT ( table_name VARCHAR2(30), total NUMBER(10) )
    CREATE TYPE TYP_TAB_TB_COUNT AS TABLE OF TYP_TB_COUNT
    CREATE OR REPLACE FUNCTION Table_Raw_Count (cur_lig in SYS_REFCURSOR)
    RETURN TYP_TAB_TB_COUNT PIPELINED
    IS
       Col   TYP_TB_COUNT := TYP_TB_COUNT (NULL,NULL) ;
       Rec   USER_TABLES%ROWTYPE ;
       Tot   NUMBER(10) ;
    Begin
       Loop
         Fetch cur_lig into Rec ;
         Exit When cur_lig%NOTFOUND ;
           Col.table_name := Rec.table_name ;
            execute immediate 'SELECT COUNT(*) FROM ' || Rec.table_name INTO Tot ;
           Col.total := Tot ;
           -- Returns the value--
           PIPE ROW( Col ) ;
        End loop ;
       Return ;
    End ;
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Production
    With the Partitioning, OLAP and Data Mining options
    SQL> Select table_name, total from table(Table_Raw_Count(CURSOR(Select * from USER_TABLES)))
      2  /
    TABLE_NAME                          TOTAL
    STOCK                                   0
    TEST_CLOB                               1
    BINAIRES                                2
    IMAGES                                  0
    TEST_ROWNUM                             4
    TEST                                    1
    TEST_TREE                               7
    TEMPERATURES                           24
    PHOTOS_EXT                              1
    PHOTOS                                  7
    MESSAGES                                7
    TABLE_NAME                          TOTAL
    MARS                                    5
    JANVIER                                 5
    FEVRIER                                 5
    EMP                                    14
    DEPT                                    4
    ARTICLES                                3
    BIN_DOCS                                2
    TRACE                                   0
    19 rows selected.
    SQL> Francois

  • Re: PLS-00455: cursor 'CUR_1' cannot be used in dynamic SQL OPEN statement

    create or replace package cognos_pk as /* Creates Package Header*/
    TYPE project_type IS record( /* A record declaration is used to */
    c1 NUMBER /* provide a definition of a record */
    ); /* that can be used by other variables*/
    TYPE project_type1 IS REF CURSOR  return project_type; /* Variable declaration */
    procedure conosg_sp (result1  out project_type1); /* SP declaration */
    end;
    CREATE OR REPLACE PACKAGE BODY cognos_pk AS /* Name of package body must be same as header */
    PROCEDURE conosg_sp(result1  OUT project_type1) IS
    countrow  number;
    BEGIN
    FOR X IN (SELECT TABLE_NAME
    FROM USER_TAB_COLUMNS
    WHERE COLUMN_NAME='PROC_STAT_CODE' AND TABLE_NAME LIKE 'INPT%')
    LOOP
    execute immediate 'select count(*)   from '||X.TABLE_NAME ||' WHERE PROC_STAT_CODE &lt;&gt;10 ' into countrow;
    --result1 := X.TABLE_NAME|| ROW_CNT;
    -- dbms_output.put_line(result1 );
    OPEN result1 for countrow;
    END loop;
    END;
    end;
    /This is my requirement ...
    I want to count the table starting with Inpt and and proc stat _code =10 which is the column name in all the table.
    i wan to return the count and the table name to be used in my cognos report.
    Edited by: BluShadow on 10-May-2013 09:22
    added {noformat}{noformat} tags around the code/data for readability (no accounting for OP's lack of formatting)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    1005181 wrote:
    create or replace package cognos_pk as /* Creates Package Header*/
    TYPE project_type IS record( /* A record declaration is used to */
    c1 NUMBER /* provide a definition of a record */
    ); /* that can be used by other variables*/
    TYPE project_type1 IS REF CURSOR  return project_type; /* Variable declaration */
    procedure conosg_sp (result1  out project_type1); /* SP declaration */
    end;
    CREATE OR REPLACE PACKAGE BODY cognos_pk AS /* Name of package body must be same as header */
    PROCEDURE conosg_sp(result1  OUT project_type1) IS
    countrow  number;
    BEGIN
    FOR X IN (SELECT TABLE_NAME
    FROM USER_TAB_COLUMNS
    WHERE COLUMN_NAME='PROC_STAT_CODE' AND TABLE_NAME LIKE 'INPT%')
    LOOP
    execute immediate 'select count(*)   from '||X.TABLE_NAME ||' WHERE PROC_STAT_CODE &lt;&gt;10 ' into countrow;
    --result1 := X.TABLE_NAME|| ROW_CNT;
    -- dbms_output.put_line(result1 );
    OPEN result1 for countrow;
    END loop;
    END;
    end;
    /This is my requirement ...
    I want to count the table starting with Inpt and and proc stat _code =10 which is the column name in all the table.
    i wan to return the count and the table name to be used in my cognos report.The execute immediate statement you have, is doing the actual count and returning the count into the variable "countrow".
    The subsequent OPEN statement for the ref cursor is trying to open a cursor, and is expecting an SQL query to do that, but you are passing it your numeric value instead.
    All you need to open a ref cursor is:
    e.g.
    OPEN result1 for 'select count(*) from '||x.table_name||' where proc_stat_code != 10';However, your code is so seriously flawed in many ways.
    a) you don't need to declare a type of "REF CURSOR" yourself. Oracle provides a type called "SYS_REFCURSOR" that you can use already.
    b) Your procedure is supplying a single ref cursor as an OUT variable, yet it tries to loop through multiple tables and re-use the same ref cursor over and over. That's not going to work. You can only pass one thing out in your OUT parameter.
    I don't know how congos expects it's results, but I assume a ref cursor is acceptable to it, so what you're actually looking for is to pass back a single ref cursor that is based on a query that can give the results for all your tables in one go.
    There are various queries that can be used to count records on multiple tables e.g.
    (ref. Laurent Schneider's blog: http://laurentschneider.com/wordpress/2007/04/how-do-i-store-the-counts-of-all-tables.html )
    SQL> select
      2    table_name,
      3    to_number(
      4      extractvalue(
      5        xmltype(
      6 dbms_xmlgen.getxml('select count(*) c from '||table_name))
      7        ,'/ROWSET/ROW/C')) count
      8  from user_tables
      9 where iot_type != 'IOT_OVERFLOW'
    10 or    iot_type is null;
    TABLE_NAME                      COUNT
    DEPT                                4
    EMP                                14
    BONUS                               0
    SALGRADE                            5You could therefore open your refcursor using a single query based on the above (adapting it for your own needs and restrictions).

  • Using a Dynamic cursor

    Hi,
    I have a problem with this code :
    DECLARE
         v_Column_Name           all_tab_columns.column_name%TYPE;      -- variable for TABLE_SQL
         v_data_type           all_tab_columns.data_type%TYPE;      -- variable for CHPS_SQL
         v_Table_Name           user_tables.table_name%type;
         lc$results     VARCHAR2(32767) := null;
         lc$Champ     VARCHAR2(32767) := null;
         lc$Requete     VARCHAR2(32767) := null;
         CURSOR C1 IS
              SELECT table_name FROM user_tables;
    BEGIN
         OPEN C1;
         LOOP
              FETCH C1 INTO V_Table_name;
              EXIT WHEN C1%NOTFOUND;
              DECLARE
                   CURSOR C2 IS
                        select column_name, data_type from all_tab_columns where table_name = V_TABLE_NAME;
              BEGIN
                   lc$results := 'INSERT INTO ' || v_table_name || '(';
                   lc$requete := 'SELECT ' ;
                   lc$Champ := NULL;
                   OPEN C2;
                   LOOP
                        FETCH C2 INTO V_column_name, V_data_type;
                        EXIT WHEN C2%NOTFOUND;
                        if lc$Champ IS NOT NULL then
                             lc$Champ := lc$champ || ',' || v_column_name;
                             lc$requete := lc$requete || ',' || '''''''''' || ',' || v_column_name || ',' || '''''''''';
                        else
                             lc$champ := lc$champ || v_column_name;
                             lc$requete := lc$requete || '''''''''' || ',' || v_column_name || ',' || '''''''''';
                        end if;
                   END LOOP;
                   CLOSE C2;
                   lc$results := lc$results || lc$champ || ') VALUES (';
                   lc$requete := lc$requete || ' FROM ' || v_table_name;
    -- MY PROBLEM BEGIN HERE
                   DECLARE
    *                    TYPE EmpCurTyp IS REF CURSOR;*
    *                    C3     EmpCurTyp;*
    *               BEGIN*
    *                    OPEN C3 FOR lc$requete;*
    *                    lc$champ := NULL;*
    *                    LOOP*
    *                         FETCH C3 INTO lc$champ ;*
    *                         Exit when C3%NOTFOUND ;*
    *                         lc$champ := lc$results || lc$champ || ')';*
    *                         dbms_output.put_line(lc$champ);*
    *                    END LOOP;*
    *                    CLOSE C3;*
    *               END;*
              END;
         END LOOP;
         CLOSE C1;
    END;          
    the error code is : ORA-00932: inconsistent datatypes: expected - got -
    Need help please,
    Thank

    904907 wrote:
    How can I use the dbms_SQL for this code?99% of the time this indicates design issues. You are trying to write "one-size-fits-all" code which by definition will have substantial overhead. Other than that, in general, you need to declare a variable for each data type possibly returned by dynamic SQL. Then use DBMS_SQL.DESCRIBE_COLUMNS to get data types of select list expressions. Fetch a row and then loop from 1 to number of select list expressions where loop body is one giant IF to test select list expression datatype and issue corresponding DBMS_SQL.COLUMN_VALUE into proper data type variable.
    SY.

  • Record counts of the tables in USER_TABLES

    Hello everyone.
    Please give me advice, is it possible to enumerate the tables in the USER_TABLES and in the same time to retrieve the record count in every each of them?
    To illustrate, I want something like that:
    select t.table_name,(select count(*) from t.table_name) from USER_TABLES t
    This is only for isllustration. I know that is not the way.
    Thank you in advance for your answers.

    Code could be compacted down a little...
    SQL> set serveroutput on
    SQL> ed
    Wrote file afiedt.buf
      1  declare
      2    cursor cur_table_count is
      3      select table_name from user_tables order by table_name;
      4    v_cnt number;
      5  begin
      6    dbms_output.put_line('Table Name                     Record Count');
      7    dbms_output.put_line('============================== ============');
      8    for t in cur_table_count
      9    loop
    10      execute immediate 'select count(*) from '||t.table_name into v_cnt;
    11      dbms_output.put_line(RPAD(t.table_name,31)||v_cnt);
    12    end loop;
    13* end;
    SQL> /
    Table Name                     Record Count
    ============================== ============
    BONUS                          0
    DEPT                           4
    EMP                            14
    SALGRADE                       5
    PL/SQL procedure successfully completed.

  • Help on cursor

    Hi Gurus,
    i write a cursor in some table. I need to findout that cursur how many records having.
    How will find?
    Regards
    Gopal

    There are many ways to get the count.
    Option 1:
    declare
    cursor cur1 is select * from user_tables;
    lv_count number;
    begin
    for i in cur1
    loop
    lv_count := CUR1%ROWCOUNT;
    end loop;
    dbms_output.put_line('Cursor Record Count ' || lv_count);
    end;
    Option 2:
    declare
    cursor cur1 is select * from user_tables;
    lv_count number := 0;
    begin
    for i in cur1
    loop
    lv_count := lv_count +1;
    end loop;
    dbms_output.put_line('Cursor Record Count ' || lv_count);
    end;
    Option 3:
    declare
    cursor cur1 is select * from user_tables;
    lv_cur1 cur1%ROWTYPE;
    lv_count number;
    begin
    open cur1;
    loop
    fetch cur1 into lv_cur1;
    exit when cur1%NOTFOUND;
    end loop;
    dbms_output.put_line('Cursor Record Count ' || CUR1%ROWCOUNT);
    end;

  • ResultSet of a cursor as an out parameter

    How can you return a cursor or a resultSet as an OUT parameter? - Any info will be greatly appreciated. A code ex will be great
    vj

    Assuming you mean as an output parameter of pl/sql procedure being called from pl/sql...
    declare
      type refcur_type is ref cursor;
      type tab_user_tables_type is table of user_tables%rowtype index by binary_integer;
      refcur_user_tables refcur_type;
      tab_user_tables tab_user_tables_type;
      row_user_tables user_tables%rowtype;
      procedure get (
        out_refcur_user_tables out refcur_type,
        out_tab_user_tables out tab_user_tables_type) is
      begin
        for row_user_tables in (select * from user_tables) loop
          out_tab_user_tables(nvl(out_tab_user_tables.count, 0) + 1) := row_user_tables;
        end loop;
        open out_refcur_user_tables for select * from user_tables;
      end get;
    begin
      get(refcur_user_tables, tab_user_tables);
      dbms_output.put_line('from table ...');
      for i in 1 .. tab_user_tables.count loop
        dbms_output.put_line(tab_user_tables(i).table_name);
      end loop;
      dbms_output.put_line('from reference cursor ...');
      loop
        fetch refcur_user_tables into row_user_tables;
        exit when refcur_user_tables%notfound;
        dbms_output.put_line(row_user_tables.table_name);
      end loop;
    end;

  • ORA-01000: Too many open cursors -- Need Help

    Hi All,
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    I am getting error ora-01000 for  the following procedures gather stats
    Could you please guide how to get-rid-off this error.
    thanks in advance;
    CREATE OR REPLACE PROCEDURE SHEMA_NAME ANALYZE_TABLES IS
       rec_table_name   VARCHAR2 (30);
       CURSOR c1
       IS
          SELECT table_name
            FROM USER_tables;  ------ 18000 table for this cursor
    BEGIN
       OPEN c1;
       LOOP
          FETCH c1 INTO rec_table_name;
          EXIT WHEN c1%NOTFOUND;
          -- block was hereÿÿÿ
          BEGIN
             DBMS_STATS.
             GATHER_TABLE_STATS (
                OWNNAME            => 'SHEMA_NAME',
                TABNAME            => rec_table_name,
                PARTNAME           => NULL,
                ESTIMATE_PERCENT   => 30,
                METHOD_OPT         => 'FOR ALL COLUMNS SIZE AUTO',
                DEGREE             => 5,
                CASCADE            => TRUE);
          END;
       END LOOP;
       CLOSE c1;
    EXCEPTION
       WHEN OTHERS
       THEN
          raise_application_error (
             -20001,
             'An error was encountered - ' || SQLCODE || ' -ERROR- ' || SQLERRM);
    END;

    Look at the following:
    SQL> begin
      2          raise no_data_found;
      3  end;
      4  /
    begin
    ERROR at line 1:
    ORA-01403: no data found
    ORA-06512: at line 2
    The error code the caller executing this code receive is -01403. A unique error number that has a known and specific meaning.
    In addition, the error stack tells the caller that this unique error occurred on line 2 in the source code.
    The caller knows EXACTLY what the error is and where it occurred.
    SQL> begin
      2          raise no_data_found;
      3  exception when OTHERS then
      4          raise_application_error(
      5                  -20000,
      6                  'oh damn some error happened. the error is '||SQLERRM
      7          );
      8  end;
      9  /
    begin
    ERROR at line 1:
    ORA-20000: oh damn some error happened. the error is ORA-01403: no data found
    ORA-06512: at line 4
    In this case the caller gets the error code -20000. It is meaningless as the same error code will be use for ALL errors (when OTHERS). So the caller will never know what the actual real error is.
    For the caller to try and figure that out, it will need to parse and process the error message text to look for the real error code. A very silly thing to do.
    In addition, the error stack says that the error was caused by line 4 in the code called.. except that this is the line that raised the meaningless generic error and not the actual line causing the error.
    There are 3 basic reasons for writing an exception handler:
    - the exception is not an error
    - the exception is a system exception (e.g. no data found) and needs to be turned into meaningful application exceptions (e.g. invoice not found, customer not found, zip code not found, etc)
    - the exception handler is used as a try..finally resource protection block (which means it re-raises the exception)
    If your exception handler cannot tick one of these three reasons for existing, you need to ask yourself why you are writing that handler.

  • How to use Execute Immediate to execute a procedure and return a cursor?

    The procedure name is unknown until run time so I have to use Execute immediate to execute the procedure, the procedure return a cursor, but I can't figure out the right syntax to pass the cursor out.
    To simplify the issue here, I create two procedures as examples.Assume I have a procedure called XDTest:
         p_cur OUT SYS_REFCURSOR
    IS
    BEGIN
    OPEN p_cur FOR
    Select * from dummy_table;
    END XDTest;
    In another procedure, I want to use Execute Immediate to execute XDTest and obtain the result that return from the cursor into a local cursor so I can process the records:
         p_cur OUT SYS_REFCURSOR
    IS
    l_cur SYS_REFCURSOR;
    BEGIN
    execute immediate 'BEGIN XDTest (:1); END;' using OUT l_cur;
    END XDTest2;
    However, this is not working, I get ORA-03113: end-of-file on communication channel error.
    What syntax should I use here?
    Cheers

    well...
    I update the XDTest2 procedure as below but when execute it get exceptions.I think the v_sqlstmt syntax is wrong, but I don't know what meant to be correct, please give some suggestions .
    Cheers
    -------------------- XDTest procedure --------------------------------------------------
         p_cur OUT SYS_REFCURSOR
    --AUTHID CURRENT_USER
    IS
    BEGIN
    OPEN p_cur FOR
    Select Table_Name from USER_Tables;
    END XDTest;
    -------------------- XDTest2 procedure --------------------------------------------------
         p_cur OUT SYS_REFCURSOR
    IS
    TYPE T1 IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER ;
    v_sqlstmt varchar2(1000):=null;
    v_cursor number;
    x_num number;
    LN$Lig number := 0 ;
    LN$MaxCol number := 0 ;
    t T1 ;
    v VARCHAR2(4000) ;
    userTb User_Tables%ROWTYPE;
    l_cur number;
    BEGIN
    LN$Lig := 0 ;
    LN$MaxCol := 1 ;
    --OPEN p_cur FOR
    --execute immediate 'BEGIN XDTest (:1); END;' using OUT l_cur;
    v_cursor:=dbms_sql.open_cursor;
    v_sqlstmt:='begin :p_cur:="XDTest"(); END; ';
    dbms_sql.parse(v_cursor,v_sqlstmt,DBMS_SQL.NATIVE);
    dbms_sql.bind_variable(v_cursor,':p_cur',l_cur);
    dbms_output.put_line('1');
    -- Define the columns --
    dbms_sql.DEFINE_COLUMN(v_cursor, 1, userTb.Table_Name, 30);
    x_num:=dbms_sql.execute(v_cursor);
    dbms_output.put_line('2');
    -- Fetch the rows from the source query --
    LOOP
    IF dbms_sql.FETCH_ROWS(v_cursor)>0 THEN
    -- get column values of the row --
    dbms_sql.COLUMN_VALUE(v_cursor, 1,userTb.Table_Name);
    dbms_output.put_line(userTb.Table_Name);
    ELSE
    -- No more rows --
    EXIT;
    END IF;
    END LOOP;
    dbms_sql.close_cursor(v_cursor);
    END XDTest2;
    ---------------------- Error when execute ------------------------------------------------
    1
    BEGIN DevD0DB.XDTest2(:l_cur); END;
    ERROR at line 1:
    ORA-01007: variable not in select list
    ORA-06512: at "SYS.DBMS_SYS_SQL", line 1010
    ORA-06512: at "SYS.DBMS_SQL", line 212
    ORA-06512: at "DEVD0DB.XDTEST2", line 35
    ORA-06512: at line 1
    ----------------------------------------------------------------------------------------------------

  • Passing value to ref cursor

    Hi,
    I have a doubt in the code mentioned below ..indicated in the code by
    " -- id shoud be the value fetched from variable v_samp;".
    I am not sure of how I get the value into the ref cursor from the variable
    declare
    Cursor C1
    Is Select Table_Name,Rownum R_Num From User_Tables Where Table_Name Like 'T%' Order By Table_Name;
    Cursor C2 Is Select Sample From code_tbl;
    Type Ref1 Is Ref Cursor;
    R_1 Ref1;
    Type T_Samp Is Table Of Varchar2(100);
    Lv_Samp T_Samp;
    lv_geno T_Samp;
    Begin
    For L1 in C1 loop
    V_tbl := L1.table_name;
    For L2 In C2 Loop
    V_samp := L2.sample;
    Open R_1 for 'Select sample_id, genotype from ' || V_tbl || ' where id = ? '; -- id shoud be the value fetched from variable v_samp;
    Loop
    Fetch R_1 Bulk Collect Into Lv_Samp,Lv_Geno Limit 10000;
    FOR indx IN 1 .. lv_samp.COUNT
    Loop
    V1 := V1 || lv_geno(indx);
    End Loop;
    EXIT WHEN Lv_samp.COUNT = 0;
    end loop;
    end loop;
    end loop;
    end;
    Thanks

    Sorry about my approach..I did that using tool. I don't know how to keep it..I do apologize if I did hurt you..
    This is my complete requirement...
    For every sample in table all_f if it exists in table t1
    then the output should be the concatenated value of type
    for the corresponding sample. Let me know if I need to do anything else..There are million types for every sample.
    The output should be
    'AB35652626-G8' 1 1 1 1 1 1 1 1 1 1 0 0
    since only this id exists in c3 (table t1).
    This is just a sample...I have huge number of rows to be concatenated for multiple id's.
    So that is the reason why I don't want to do any processing in the loop except concatenation.
    DDL's and DML's are as follows :
    create table all_f (sample varchar2(20), typeo varchar2(20));
    insert into all_f (SAMPLE, typeo)
    values ('AB35053903-C10', '2');
    insert into all_f (SAMPLE, typeo)
    values ('AB35053995-A10', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35054283-C3', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35054582-A7', '2');
    insert into all_f (SAMPLE, typeo)
    values ('AB35055053-H12', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35055158-B2', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35500856-F4', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35501332-G11', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35501428-B9', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35504972-F11', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35505551-H7', '2');
    insert into all_f (SAMPLE, typeo)
    values ('AB35506138-G5', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35507097-C11', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35507190-G9', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35561723-H10', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35651275-E6', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35896175-C8', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35897805-A3', '2');
    insert into all_f (SAMPLE, typeo)
    values ('AB35899249-H8', '1');
    insert into all_f (SAMPLE, typeo)
    values ('AB35899918-H6', '1');
    insert into t1 (SAMPLE, TYPEO)
    values ('AB35652626-G8', '1');
    commit;
    create table t1 (sample_id varchar2(20), type varchar2(20));
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '1');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '0');
    insert into t1 (SAMPLE_ID, TYPE)
    values ('AB35652626-G8', '0');
    Thanks,

  • Windows 7 displays error message when exiting +cursor issue

    Two issues here. CS5 Phoshop on Wind 7 64 bit.
    Physical processor count: 8
    Processor speed: 3073 MHz
    Built-in memory: 12279 MB
    Free memory: 9577 MB
    Memory available to Photoshop: 10934 MB
    Memory used by Photoshop: 80 %
    Image tile size: 128K
    First issue is since the latest automatic Adobe update (why fix what isn't broken?) Every time I now exit Photoshop I get the message "Adobe QT Server has stoped working" and occasionally it happens when I exit bridge. Indesign is also behaving badly. I can no longer start a previous document from file manager without ID crashing out.
    The other is the cursors in Clone and erase lose their edge (become invisable) for no reason - well not quite. Noise Ninja crashed Photoshop when I tried to use it. I reinstalled it and all is well. The cursor issue seems to be intermittant but came back (for no reason) after I reinstalled NN. I can't seem to change the cursor, no matter what I do. The problem is now seriously affecting how I work. Almost enough to go back to Win XP which ran CS5 Photoshop flawlessly.
    Any help will be gratefully accepted.
    Doug

    function(){return A.apply(null,[this].concat($A(arguments)))}
    doug87510 wrote:
    The recent problem is the entire outline of the cursor (including the crosshair in the middle) was missing at any size of cursor. All I had was exactly what I'd get if I used a real spraygun.
    Well, that issue is simply a matter of hitting the Caps Lock key.  When Caps Lock is on, you'll see the cursor outline, and when it is off you'll see a crosshair.  That's a feature, not a bug.
    Glad to hear the 11.1 drivers are out.  I will download them and try them now myself.
    Regarding "Adobe QT" crashing...  QT brings to mind QuickTime, though that is Apple, not Adobe.  Do you have Apple QuickTime installed?
    Regarding memory usage, with 12 GB of installed RAM, you should be able to set Photoshop to use 90% or more in Edit - Preferences - Performance.
    -Noel

  • Help with if statement in cursor and for loop to get output

    I have the following cursor and and want to use if else statement to get the output. The cursor is working fine. What i need help with is how to use and if else statement to only get the folderrsn that have not been updated in the last 30 days. If you look at the talbe below my select statement is showing folderrs 291631 was updated only 4 days ago and folderrsn 322160 was also updated 4 days ago.
    I do not want these two to appear in my result set. So i need to use if else so that my result only shows all folderrsn that havenot been updated in the last 30 days.
    Here is my cursor:
    /*Cursor for Email procedure. It is working Shows userid and the string
    You need to update these folders*/
    DECLARE
    a_user varchar2(200) := null;
    v_assigneduser varchar2(20);
    v_folderrsn varchar2(200);
    v_emailaddress varchar2(60);
    v_subject varchar2(200);
    Cursor c IS
    SELECT assigneduser, vu.emailaddress, f.folderrsn, trunc(f.indate) AS "IN DATE",
    MAX (trunc(fpa.attemptdate)) AS "LAST UPDATE",
    trunc(sysdate) - MAX (trunc(fpa.attemptdate)) AS "DAYS PAST"
    --MAX (TRUNC (fpa.attemptdate)) - TRUNC (f.indate) AS "NUMBER OF DAYS"
    FROM folder f, folderprocess fp, validuser vu, folderprocessattempt fpa
    WHERE f.foldertype = 'HJ'
    AND f.statuscode NOT IN (20, 40)
    AND f.folderrsn = fp.folderrsn
    AND fp.processrsn = fpa.processrsn
    AND vu.userid = fp.assigneduser
    AND vu.statuscode = 1
    GROUP BY assigneduser, vu.emailaddress, f.folderrsn, f.indate
    ORDER BY fp.assigneduser;
    BEGIN
    FOR c1 IN c LOOP
    IF (c1.assigneduser = v_assigneduser) THEN
    dbms_output.put_line(' ' || c1.folderrsn);
    else
    dbms_output.put(c1.assigneduser ||': ' || 'Overdue Folders:You need to update these folders: Folderrsn: '||c1.folderrsn);
    END IF;
    a_user := c1.assigneduser;
    v_assigneduser := c1.assigneduser;
    v_folderrsn := c1.folderrsn;
    v_emailaddress := c1.emailaddress;
    v_subject := 'Subject: Project for';
    END LOOP;
    END;
    The reason I have included the folowing table is that I want you to see the output from the select statement. that way you can help me do the if statement in the above cursor so that the result will look like this:
    emailaddress
    Subject: 'Project for ' || V_email || 'not updated in the last 30 days'
    v_folderrsn
    v_folderrsn
    etc
    [email protected]......
    Subject: 'Project for: ' Jim...'not updated in the last 30 days'
    284087
    292709
    [email protected].....
    Subject: 'Project for: ' Kim...'not updated in the last 30 days'
    185083
    190121
    190132
    190133
    190159
    190237
    284109
    286647
    294631
    322922
    [email protected]....
    Subject: 'Project for: Joe...'not updated in the last 30 days'
    183332
    183336
    [email protected]......
    Subject: 'Project for: Sam...'not updated in the last 30 days'
    183876
    183877
    183879
    183880
    183881
    183882
    183883
    183884
    183886
    183887
    183888
    This table is to shwo you the select statement output. I want to eliminnate the two days that that are less than 30 days since the last update in the last column.
    Assigneduser....Email.........Folderrsn...........indate.............maxattemptdate...days past since last update
    JIM.........      jim@ aol.com.... 284087.............     9/28/2006.......10/5/2006...........690
    JIM.........      jim@ aol.com.... 292709.............     3/20/2007.......3/28/2007............516
    KIM.........      kim@ aol.com.... 185083.............     8/31/2004.......2/9/2006.............     928
    KIM...........kim@ aol.com.... 190121.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190132.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190133.............     2/9/2006.........2/9/2006.............928
    KIM...........kim@ aol.com.... 190159.............     2/13/2006.......2/14/2006............923
    KIM...........kim@ aol.com.... 190237.............     2/23/2006.......2/23/2006............914
    KIM...........kim@ aol.com.... 284109.............     9/28/2006.......9/28/2006............697
    KIM...........kim@ aol.com.... 286647.............     11/7/2006.......12/5/2006............629
    KIM...........kim@ aol.com.... 294631.............     4/2/2007.........3/4/2008.............174
    KIM...........kim@ aol.com.... 322922.............     7/29/2008.......7/29/2008............27
    JOE...........joe@ aol.com.... 183332.............     1/28/2004.......4/23/2004............1585
    JOE...........joe@ aol.com.... 183336.............     1/28/2004.......3/9/2004.............1630
    SAM...........sam@ aol.com....183876.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183877.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183879.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183880.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183881.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183882.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183883.............3/5/2004.........3/8/2004.............1631
    SAM...........sam@ aol.com....183884.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183886.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183887.............3/5/2004.........3/8/2004............     1631
    SAM...........sam@ aol.com....183888.............3/5/2004.........3/8/2004............     1631
    PAT...........pat@ aol.com.....291630.............2/23/2007.......7/8/2008............     48
    PAT...........pat@ aol.com.....313990.............2/27/2008.......7/28/2008............28
    NED...........ned@ aol.com.....190681.............4/4/2006........8/10/2006............746
    NED...........ned@ aol.com......95467.............6/14/2006.......11/6/2006............658
    NED...........ned@ aol.com......286688.............11/8/2006.......10/3/2007............327
    NED...........ned@ aol.com.....291631.............2/23/2007.......8/21/2008............4
    NED...........ned@ aol.com.....292111.............3/7/2007.........2/26/2008............181
    NED...........ned@ aol.com.....292410.............3/15/2007.......7/22/2008............34
    NED...........ned@ aol.com.....299410.............6/27/2007.......2/27/2008............180
    NED...........ned@ aol.com.....303790.............9/19/2007.......9/19/2007............341
    NED...........ned@ aol.com.....304268.............9/24/2007.......3/3/2008............     175
    NED...........ned@ aol.com.....308228.............12/6/2007.......12/6/2007............263
    NED...........ned@ aol.com.....316689.............3/19/2008.......3/19/2008............159
    NED...........ned@ aol.com.....316789.............3/20/2008.......3/20/2008............158
    NED...........ned@ aol.com.....317528.............3/25/2008.......3/25/2008............153
    NED...........ned@ aol.com.....321476.............6/4/2008.........6/17/2008............69
    NED...........ned@ aol.com.....322160.............7/3/2008.........8/21/2008............4
    MOE...........moe@ aol.com.....184169.............4/5/2004.......12/5/2006............629
    [email protected]/27/2004.......3/8/2004............1631
    How do I incorporate a if else statement in the above cursor so the two days less than 30 days since last update are not returned. I do not want to send email if the project have been updated within the last 30 days.
    Edited by: user4653174 on Aug 25, 2008 2:40 PM

    analytical functions: http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/functions2a.htm#81409
    CASE
    http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/02_funds.htm#36899
    http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96624/04_struc.htm#5997
    Incorporating either of these into your query should assist you in returning the desired results.

Maybe you are looking for