Dynamic Spatial Cursor?

I have a working cursor which selects and operates on some spatial data.
The cursor uses the GETVERTICES spatial function.
The current query works only for geom_id = 347.
I need to iterate through all the geometries in the database i.e. repeat the query for geom_id = 1. geom_id = 2 ... etcetera.
Given that a cursor is defined in the declaration section, how can I dynamically change the geom_id and rerun the query for all geometries?
declare
...stuff
cursor c1 is
SELECT t.X, t.Y, ne_id
FROM SDO_TABLE c,
TABLE(SDO_UTIL.GETVERTICES(c.geom)) t
where geom_id = 347;
begin
open c1;
fetch ...stuff
loop
...do stuff with fetched data
end loop;
close c1;
end;

Hi,
I hope the below parameterized cursor will help you:
DECLARE
CURSOR c1(p_num NUMBER )
IS
SELECT * FROM A
WHERE Rd = p_num;
R1 c1%ROWTYPE;
BEGIN
-- fetch genom number from other cursor OR if its static calue then call the above cursor as many times
OPEN c1(200);
LOOP
FETCH c1 INTO r1;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(r1.sd );
END LOOP;
END;
~Vinod

Similar Messages

  • Dynamic Ref Cursor report in 6i

    Hello,
    I apologise that this question is so similar to many in this forum, but I haven't found a solution to my problem yet.
    I've created a ref cursor query because I need to include a variable p_orgs in my WHERE clause, like so:
    where e.org_id in ('||p_orgs||')
    I've constructed the report as mentioned on this page:
    www.dulcian.com
    FAQs - SQL & PL/SQL FAQs - FAQ ID# 5:
    "How can you use 'dynamic' ref cursors in Oracle Reports 3.0 / 6i?"
    (Thanks to Zlatko Sirotic for the information).
    I get a compile error when compiling the package body that holds my ref cursor open statement:
    Error 103 Encountered the symbol ''select'' when expecting one of the following:
    Select
    Is there any way around this problem?
    If I was to upgrade to Reports 9i would it work?
    Many thanks,
    Hazel

    Hello,
    Just a remark : you don't need to use a Ref Cursor if you just want to use a "dynamic where clause".
    You can use a lexical reference :
    You can find examples at :
    http://www.oracle.com/webapps/online-help/reports/10.1.2/topics/htmlhelp_rwbuild_hs/rwwhthow/whatare/dmobj/sq_a_lexical_references.htm
    (This page is about Reports 10.1.2 but Lexical references are identical in Reports 6i and Reports 10.1.2)
    Regards

  • Need help on Dynamic SQL Cursor in Forms

    Hi All,
    I am trying to execute Dynamic SQL Cursor in forms using EXEC_SQL built in.
    I have a cursor for example:
    'select * from supplier where supplier = '||p_supplier||' and processing_order = '||p_order
    My code is
    cur_num := Exec_SQL.Open_cursor;
    sql_order := 'select * from supplier where supplier = '||p_supplier||' and processing_order = '||p_order;
    EXEC_SQL.PARSE(cursor_number, sql_order);
      EXEC_SQL.DEFINE_COLUMN(cur_num ,1,ln_Supp_Id);
      EXEC_SQL.DEFINE_COLUMN(cur_num ,2,ls_Suppl_Name,30);
    EXEC_SQL.DEFINE_COLUMN(cur_num ,24,ls_exchange,20);
      sql_count := EXEC_SQL.EXECUTE(cur_num );
      While EXEC_SQL.FETCH_ROWS(cur_num ) > 0 Loop
            EXEC_SQL.COLUMN_VALUE(cur_num ,1,ln_Supp_Id);
            EXEC_SQL.COLUMN_VALUE(cur_num ,2,ls_Suppl_Name);
            EXEC_SQL.COLUMN_VALUE(cur_num ,24,ls_exchange);
    End Loop;
    EXEC_SQL.CLOSE_CURSOR(cur_num );
    In this case I have to write 24 Define Columns and 24 Column value. Is there any way to assign them to %rowtype at one time as I need all coulmn of the table.
    I had similar case on multiple tables.
    Please help me
    Thanks,
    Maddy

    I need this dynamic sql because p_supplier and p_order values changes at run time
    I do not understand. Is this a simplified sample or the real thing? You do know that you can pass variables to cursors:
    cursor test is
    select * from supplier where supplier = p_supplier and processing_order = p_order;
    or does e.g. p_supplier hold other parts of the query?
    cheers

  • How to out Dynamic ref cursor from Procedure to Forms

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

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

  • Tunning PL/SQL  with sql dynamic returning cursor

    Hi
    How can I to tunning a Stored Procedure with Dynamic Cursor that return a Cursor (resultset to VB), is very slow

    1. Run your app (or better yet your PL/SQL procedure) with SQL_TRACE=TRUE and analyze trace file (TKPROF report e.g.)
    2. Tune SQL executed by PL/SQL procedure directly. Copy the SQL from the procedure, run it in SQL*Plus [with SET AUTOTRACE ON] and analyze exec plan, statistics etc.
    3. profile your procedure to figure out which part of it takes what (time, resources etc.)

  • Oracle to MS SQL porting - dynamic sql - cursors

    I have a stored procedure written for oracle which needs to be ported to MS SQL. Used SSMA to do that but the dynamic sql statements are not getting ported and are the same. Below is a dynamic sql from the SP after making changes with SSMA.
    SET @dsql_statement =
    'declare '
    +
    ' p table_name%rowtype;'
    +
    ' c table_name%rowtype;'
    +
    ' TYPE DAT IS REF CURSOR;'
    +
    ' type dtt is table of table_name1%rowtype index by binary_integer;'
    +
    ' d dtt;'
    +
    ' aRC DAT;'
    +
    ' sq varchar2(1000);'
    +
    ' i NUMBER:=0;'
    +
    ' attributeLabel nvarchar2(255);'
    +
    ' attributename nvarchar2(255);'
    +
    ' attributetype nvarchar2(255);'
    +
    ' fkvalue nvarchar2(255);'
    +
    ' defaultfield nvarchar2(80);'
    +
    ' tablename nvarchar2(80);'
    +
    ' primarykeyfield nvarchar2(80);'
    +
    ' begin '
    +
    ' sq:=''SELECT * FROM mdbadmin.table_name WHERE lower(COLUMN1) IN'
    +
    ' (''''update'''',''''insert'''') AND COLUMN2 >= '
    +
    ISNULL(CAST(@COLUMN2 AS nvarchar(max)), '')
    +
    +
    ' ORDER BY COLUMN3,COLUMN2 ASC'';'
    +
    ' open aRC for sq;'
    +
    ' fetch aRC into c;'
    +
    ' loop '
    +
    ' fetch aRC into p;'
    +
    ' exit when aRC%NOTFOUND;'

    Some thing like
    dsql_statement :=  'begin '
    || ' for I in (SELECT * FROM mdbadmin.table_name WHERE lower(COLUMN1) IN '
    || ' (''update'',''insert'') AND COLUMN2 >= '
    || 'NVL(to_char('||'@COLUMN2'||'), '''') '
    || ' ORDER BY COLUMN3,COLUMN2 ASC ) '
    || 'loop '
    || 'end loop; '
    || 'end; '
    you can use for loop instead

  • Dynamic Ref Cursor with Dynamic Fetch

    Hi,
    I'm using dynamic sql (DBMS_SQL) to define columns of ref cursor.
    It works Ok but the problem is when i'm using PL/SQL CURSOR in the REF CURSOR. Then,
    I'm getting :
    Error at line 3
    ORA-00932: inconsistent datatypes: expected NUMBER got CURSER
    ORA-06512: at "SYS.DBMS_SQL", line 1830
    ORA-06512: at "TW.PRINT_REF_CURSOR", line 28
    ORA-06512: at line 9
    Here is my code:
    set serveroutput on
    exec DBMS_OUTPUT.ENABLE(1000000);
    declare
    l_cursor sys_refcursor;
    begin
    OPEN l_cursor FOR
    SELECT SERVICE_TABLE.SERVICE, SERVICE_TABLE.SERVICE_GROUP, SERVICE_TABLE.SERVICE_DESC,
    CURSOR(SELECT SERVICE_TABLE.SERVICE_CD FROM SERVICE_TABLE) SERVICE_CD_CURSOR
    FROM SERVICE_TABLE ;
    print_ref_cursor( l_cursor );
    end;
    =========================
    CREATE OR REPLACE procedure print_ref_cursor
    ( p_query in out sys_refcursor,
    p_date_fmt in varchar2 default 'dd-mon-yyyy hh24:mi:ss' )
    is
    l_theCursor integer;
    l_columnValue varchar2(4000);
    l_descTbl dbms_sql.desc_tab2;
    l_colCnt number;
    l_date date;
    l_cursor SYS_REFCURSOR;
    begin
    l_theCursor := dbms_sql.to_cursor_number( p_query );
    dbms_sql.describe_columns2
    ( l_theCursor, l_colCnt, l_descTbl );
    -- define all columns to be cast to varchar2's, we
    -- are just printing them out
    for i in 1 .. l_colCnt loop
    if ( l_descTbl(i).col_type in ( 12, 178, 179, 180, 181, 231 ) )
    then
    dbms_sql.define_column
    (l_theCursor, i, l_date );
    else
    dbms_sql.define_column
    (l_theCursor, i, l_columnValue, 4000);
    end if;
    end loop;
    while ( dbms_sql.fetch_rows(l_theCursor) > 0 )
    loop
    for i in 1 .. l_colCnt loop
    if ( l_descTbl(i).col_type in ( 12, 178, 179, 180, 181, 231 ) )
    then
    dbms_sql.column_value( l_theCursor, i, l_date );
    l_columnValue := to_char( l_date, p_date_fmt );
    else
    dbms_sql.column_value( l_theCursor, i, l_columnValue );
    end if;
    dbms_output.put_line
    ( rpad( l_descTbl(i).col_schema_name || '.' ||
    l_descTbl(i).col_name, 30 ) || ': ' || l_columnValue );
    end loop;
    dbms_output.put_line( '-----------------' );
    end loop;
    dbms_sql.close_cursor( l_theCursor );
    end;
    Is there a solution or bypass?
    Regards,
    Tamir Geva

    No. The problem is that one cannot use DBMS_SQL.define_column() to define that column in the SQL projection as a cursor, and then use DBMS_SQL.column_value() to read it into a ref cursor variable.
    You can however detect the cursor column - the DBMS_SQL.describe_columns3() call will return a col_type value of 102. In which case you can treat it as an exception (i.e. not process that column in the projection).
    As a general issue - a cursor as a SQL column projection does not make sense to me. I have never used this in any production code. Nor do I see any reasons why.
    If you want that column in the projection to contain a "list" of sorts (the results of the cursor), then a nested table type can be used as projected type and the MultiSet() function used to execute the in-line SQL and provide that SQL cursor's result as an array/nested table.
    But even this approach raises the question why a standard relational join is not used?

  • Dynamic REF Cursor with Dynamic Fetch - Urgent

    i have a pl/sql package with generates dynamic SQL statments. my problem is i want to open this SQL statment dynamically and fetch data in dynamic variable.
    declare
    type type_temp is REF CURSOR;
    cur_temp type_temp;
    mv_sql varchar2(4000);
    begin
    -- this will be dunamically generated and
    -- hence could have any no. of columns.
    mv_sql := select f1, f2, f3, f4 from table_temp;
    open cur_temp for mv_sql;
    fetch cur_temp into c1, c2, c3, c4;
    close cur_temp;
    end;
    problem is my sql statment will have N no. of columns how can i fetch this N no. of columns.

    Very hard problem, because ref cursors do not (directly) support description!
    Se mine (non-ideal) solution (it may be doable, but it isn't very practical
    or easily maintainable):
    1. "Generic" package
    CREATE OR REPLACE PACKAGE dyn_fetch IS
    TYPE ref_cur_t IS REF CURSOR;
    g_query VARCHAR2 (32000);
    g_count NUMBER;
    g_desc_tab DBMS_SQL.DESC_TAB;
    varchar2_type CONSTANT PLS_INTEGER := 1;
    number_type CONSTANT PLS_INTEGER := 2;
    date_type CONSTANT PLS_INTEGER := 12;
    rowid_type CONSTANT PLS_INTEGER := 11;
    char_type CONSTANT PLS_INTEGER := 96;
    long_type CONSTANT PLS_INTEGER := 8;
    raw_type CONSTANT PLS_INTEGER := 23;
    mlslabel_type CONSTANT PLS_INTEGER := 106;
    clob_type CONSTANT PLS_INTEGER := 112;
    blob_type CONSTANT PLS_INTEGER := 113;
    bfile_type CONSTANT PLS_INTEGER := 114;
    PROCEDURE describe_columns;
    FUNCTION record_def RETURN VARCHAR2;
    END;
    CREATE OR REPLACE PACKAGE BODY dyn_fetch IS
    PROCEDURE describe_columns IS
    l_cur INTEGER;
    BEGIN
    l_cur := DBMS_SQL.OPEN_CURSOR;
    DBMS_SQL.PARSE (l_cur, g_query, DBMS_SQL.NATIVE);
    DBMS_SQL.DESCRIBE_COLUMNS (l_cur, g_count, g_desc_tab);
    DBMS_SQL.CLOSE_CURSOR (l_cur);
    EXCEPTION
    WHEN OTHERS THEN
    IF DBMS_SQL.IS_OPEN (l_cur) THEN
    DBMS_SQL.CLOSE_CURSOR (l_cur);
    END IF;
    RAISE;
    END;
    FUNCTION record_def RETURN VARCHAR2 IS
    l_record_def VARCHAR2 (32000);
    l_type VARCHAR2 (100);
    l_col_type PLS_INTEGER;
    l_col_max_len PLS_INTEGER;
    l_col_precision PLS_INTEGER;
    l_col_scale PLS_INTEGER;
    BEGIN
    FOR i IN 1..g_count LOOP
    l_col_type := g_desc_tab(i).col_type;
    l_col_max_len := g_desc_tab(i).col_max_len;
    l_col_precision := g_desc_tab(i).col_precision;
    l_col_scale := g_desc_tab(i).col_scale;
    IF l_col_type = varchar2_type THEN
    l_type := 'VARCHAR2(' || l_col_max_len || ')';
    ELSIF l_col_type = number_type THEN
    l_type := 'NUMBER(' || l_col_precision || ',' || l_col_scale || ')';
    ELSIF l_col_type = date_type THEN
    l_type := 'DATE';
    ELSIF l_col_type = rowid_type THEN
    l_type := 'ROWID';
    ELSIF l_col_type = char_type THEN
    l_type := 'CHAR(' || l_col_max_len || ')';
    -- ELSIF l_col_type = ...
    -- long_type, raw_type ...
    END IF;
    l_record_def := l_record_def || ' col_' || i || ' ' || l_type || ',';
    END LOOP;
    l_record_def := RTRIM (l_record_def, ',');
    RETURN l_record_def;
    END;
    END;
    Note that procedure "record_def" creates columns names as col_1 (col_2 ...)
    because SELECT clause in your query can be without aliases, for example
    "SELECT deptno || dname FROM dept".
    2. Your package which returns query nad ref cursor
    CREATE OR REPLACE PACKAGE test IS
    PROCEDURE set_query (p_query VARCHAR2 := NULL);
    FUNCTION ref_cur RETURN dyn_fetch.ref_cur_t;
    END;
    CREATE OR REPLACE PACKAGE BODY test IS
    PROCEDURE set_query (p_query VARCHAR2 := NULL) IS
    l_query VARCHAR2 (32000) :=
    ' SELECT e.empno, e.ename,' ||
    ' e.deptno, d.dname' ||
    ' FROM emp e,' ||
    ' dept d' ||
    ' WHERE e.deptno = d.deptno';
    BEGIN
    IF p_query IS NULL THEN
    dyn_fetch.g_query := l_query;
    ELSE
    dyn_fetch.g_query := p_query;
    END IF;
    END;
    FUNCTION ref_cur RETURN dyn_fetch.ref_cur_t IS
    l_ref_cur dyn_fetch.ref_cur_t;
    BEGIN
    OPEN l_ref_cur FOR dyn_fetch.g_query;
    RETURN l_ref_cur;
    END;
    END;
    Why we need two separate procedures (functions) in your package ?
    a) Receiving program must use dynamic SQL, but in dynamic block we can access
    only PL/SQL code elements that have global scope (standalone functions and procedures,
    and elements defined in the specification of a package).
    Unfortunately, cursor variables cannot be defined in the specification of a package
    (cannot be global variables).
    b) Receiving program must get the column list before ref cursor.
    So, we have two options: call (in receiving program) the same function two times
    (once to get the column list and once to return a ref cursor)
    or use one procedure (or function) for returning query (to get the column list)
    and second function for returning a ref cursor.
    3. Your receiving program
    CREATE OR REPLACE PROCEDURE test_fetch_ref_cur (p_query VARCHAR2 := NULL) IS
    l_statement VARCHAR2 (32000);
    FUNCTION process_def RETURN VARCHAR2 IS
    l_process_def VARCHAR2 (32000);
    BEGIN
    l_process_def := 'DBMS_OUTPUT.PUT_LINE (';
    FOR i IN 1 .. dyn_fetch.g_count LOOP
    l_process_def := l_process_def || ' l_record.col_' || i || ' || ''>>'' || ';
    END LOOP;
    l_process_def := RTRIM (l_process_def, ' || ''>>'' || ') || ');';
    RETURN l_process_def;
    END;
    BEGIN
    test.set_query (p_query);
    dyn_fetch.describe_columns;
    l_statement :=
    ' DECLARE' ||
    ' TYPE record_t IS RECORD (' ||
    dyn_fetch.record_def || ');' ||
    ' l_record record_t;' ||
    ' l_ref_cur dyn_fetch.ref_cur_t;' ||
    ' BEGIN' ||
    ' l_ref_cur := test.ref_cur;' ||
    ' LOOP' ||
    ' FETCH l_ref_cur INTO l_record;' ||
    ' EXIT WHEN l_ref_cur%NOTFOUND;' ||
    process_def ||
    ' END LOOP;' ||
    ' CLOSE l_ref_cur;' ||
    ' END;';
    EXECUTE IMMEDIATE l_statement;
    END;
    You can test this with:
    SET SERVEROUTPUT ON;
    EXECUTE test_fetch_ref_cur;
    Note that we can try to use more generic solution:
    CREATE OR REPLACE PACKAGE dyn_fetch IS
    -- SAME AS BEFORE, PLUS:
    PROCEDURE fetch_ref_cur (
    p_function_ref_cur VARCHAR2,
    p_process_def VARCHAR2);
    END;
    CREATE OR REPLACE PACKAGE BODY dyn_fetch IS
    -- SAME AS BEFORE, PLUS:
    PROCEDURE fetch_ref_cur (
    p_function_ref_cur VARCHAR2,
    p_process_def VARCHAR2)
    IS
    l_statement VARCHAR2 (32000);
    BEGIN
    l_statement :=
    ' DECLARE' ||
    ' TYPE record_t IS RECORD (' ||
    record_def || ');' ||
    ' l_record record_t;' ||
    ' l_ref_cur dyn_fetch.ref_cur_t;' ||
    ' BEGIN' ||
    ' l_ref_cur := ' ||
    p_function_ref_cur || ';' ||
    ' LOOP' ||
    ' FETCH l_ref_cur INTO l_record;' ||
    ' EXIT WHEN l_ref_cur%NOTFOUND;' ||
    p_process_def ||
    ' END LOOP;' ||
    ' CLOSE l_ref_cur;' ||
    ' END;';
    EXECUTE IMMEDIATE l_statement;
    END;
    END;
    CREATE OR REPLACE PROCEDURE test_fetch_ref_cur (p_query VARCHAR2 := NULL) IS
    FUNCTION process_def RETURN VARCHAR2 IS
    -- SAME AS BEFORE
    END;
    BEGIN
    test.set_query (p_query);
    dyn_fetch.describe_columns;
    dyn_fetch.fetch_ref_cur (
    p_function_ref_cur => 'test.ref_cur',
    p_process_def => process_def);
    END;
    Regards,
    Zlatko Sirotic

  • Error in dynamic ref cursor

    hi
    i tried the following but getting the error.. Could you help me in this please
    SQL> create or replace package curpack as
      2    type T_CUR is ref cursor;
      3    procedure CHECKDYNREF (io_cur out T_CUR);
      4  end curpack;
      5  /
    Package created.
    SQL>
    SQL> create or replace package body curpack as
      2    procedure CHECKDYNREF (io_cur out T_CUR);
      3    is
      4     begin
      5       open io_cur for 'SELECT * FROM EMP';
      6    end;
      7  end curpack;
      8  /
    Warning: Package Body created with compilation errors.
    SQL> sho err
    Errors for PACKAGE BODY CURPACK:
    LINE/COL ERROR
    3/3      PLS-00103: Encountered the symbol "IS" when expecting one of the
             following:
             begin end function package pragma procedure subtype type use
             <an identifier> <a double-quoted delimited-identifier> form
             current cursor
    7/1      PLS-00103: Encountered the symbol "END"Thanks...
    Edited by: josh1612 on Jan 24, 2010 9:05 PM

    Hi,
    There should not be a ';' (semi colon ) after the declaration of procedure in package body
    SQL> create or replace package body curpack as
          procedure CHECKDYNREF (io_cur out T_CUR);
          is
           begin
             open io_cur for 'SELECT * FROM EMP';
          end;
        end curpack;
        / Something like above
    Regards

  • Dynamic sql and ref cursors URGENT!!

    Hi,
    I'm using a long to build a dynamic sql statement. This is limited by about 32k. This is too short for my statement.
    The query results in a ref cursor.
    Does anyone have an idea to create larger statement or to couple ref cursors, so I can execute the statement a couple of times and as an result I still have one ref cursor.
    Example:
    /* Determine if project is main project, then select all subprojects */
    for i in isMainProject loop
    if i.belongstoprojectno is null then
    for i in ProjectSubNumbers loop
    if ProjectSubNumbers%rowcount=1 then
    SqlStatement := InitialStatement || i.projectno;
    else
    SqlStatement := SqlStatement || PartialStatement || i.projectno;
    end if;
    end loop;
    else
    for i in ProjectNumber loop
    if ProjectNumber%rowcount=1 then
    SqlStatement := InitialStatement || i.projectno;
    else
    SqlStatement := SqlStatement || PartialStatement || i.projectno;
    end if;
    end loop;
    end if;
    end loop;
    /* Open ref cursor */
    open sql_output for SqlStatement;
    Thanks in advance,
    Jeroen Muis
    KCI Datasystems BV
    mailto:[email protected]

    Example for 'dynamic' ref cursor - dynamic WHERE
    (note that Reports need 'static' ref cursor type
    for building Report Layout):
    1. Stored package
    CREATE OR REPLACE PACKAGE report_dynamic IS
    TYPE type_ref_cur_sta IS REF CURSOR RETURN dept%ROWTYPE; -- for Report Layout only
    TYPE type_ref_cur_dyn IS REF CURSOR;
    FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn;
    END;
    CREATE OR REPLACE PACKAGE BODY report_dynamic IS
    FUNCTION func_dyn (p_where VARCHAR2) RETURN type_ref_cur_dyn IS
    ref_cur_dyn type_ref_cur_dyn;
    BEGIN
    OPEN ref_cur_dyn FOR
    'SELECT * FROM dept WHERE ' | | NVL (p_where, '1 = 1');
    RETURN ref_cur_dyn;
    END;
    END;
    2. Query PL/SQL in Reports
    function QR_1RefCurQuery return report_dynamic.type_ref_cur_sta is
    begin
    return report_dynamic.func_dyn (:p_where);
    end;
    Regards
    Zlatko Sirotic
    null

  • Dynamically appending records to an existing cursor---URGENT !!!

    Hi all,
    I'm right now creating dynamic statistical reports in Oracle 8.1.6 on Win NT.
    My query is as below:
    select
         TO_CHAR(a.jobpostingdate,'MM-DD-YYYY') jobpostingdate ,count(a.jobpostingid) no_jobs
    FROM
         JOBPOSTING a, EMPLOYEE f
    WHERE
         a.employeeid = f.employeeid AND
         f.companyid = 2 AND
         a.jobpostingdate between to_date('10-01-2002 00:00:00','mm-dd-yyyy hh24:mi:ss') AND
         to_date('10-01-2003 00:00:00','mm-dd-yyyy hh24:mi:ss')
    GROUP BY
         TO_CHAR(a.jobpostingdate,'MM-DD-YYYY')
    ORDER BY
         TO_CHAR(a.jobpostingdate,'MM-DD-YYYY')     
    This query will give me records for everyday. But if there is no record for a certain date, then I don't get that record. This is how it works.
    The result of the above query is as below:
    jobpostingdate no_jobs
    10-11-2002 1
    10-12-2002 5
    10-15-2002 4
    10-16-2002 2
    In the above resultset, There are no records displayed for 10-13-2002 and 10-14-2002. But my requirement is that I need both the records for 10-13-2002 and 10-14-2002 with the count as 0.
    Can anybody please tell me how I can do this? Is there a way in Oracle 8.1.6? And if I need to write a function for this how do I do it ? I'm not able to find any way to do this. Or Do I need to dynamically appened cursor or something? How do I solve this problem?
    Please give me a solution as soon as possible...this is very URGENT for me !!!
    Thanx,
    Madhavi

    I did not test this, but it should work.
    I understand from your posting that the missing dates are existing in the table but there is no match on these rows with employeeid join. If this is correct then the following should work,SELECT * FROM
    Select TO_CHAR(a.jobpostingdate,'MM-DD-YYYY') jobpostingdate, count(*) no_jobs
    FROM JOBPOSTING a, EMPLOYEE e
    WHERE e.companyid = 2
    AND   a.employeeid = e.employeeid
    AND   a.jobpostingdate between
          to_date('10-01-2002 00:00:00','mm-dd-yyyy hh24:mi:ss') AND
          to_date('10-01-2003 00:00:00','mm-dd-yyyy hh24:mi:ss')
    GROUP BY TO_CHAR(a.jobpostingdate,'MM-DD-YYYY')
    UNION
    Select TO_CHAR(a.jobpostingdate,'MM-DD-YYYY') jobpostingdate, 0 no_jobs
    FROM JOBPOSTING a
    WHERE NOT EXISTS( Select 'x' From EMPLOYEE e
                      WHERE e.companyid = 2 -- you might not need this condition
                AND   e.employeeid = a.employeeid)
    ORDER BY jobpostingdate;Thx,
    SriDHAR

  • Dynamically defining a cursor statement

    I was hoping to find help with coding a dynamically defined cursor statement, as some of the tables in the 'FROM' clause of the cursor definition are not known until runtime. Thanks in advance for your help!
    Cheers,
    Vladimir

    Hi,
    I think Your problem can be solved
    using REF cursors which are defined
    within the Begin block rather than
    the Declare block. See if the below
    code helps.
    Bye,
    Srinidhi
    PROCEDURE proc_xyz ( av_input IN VARCHAR2 ) IS
    lv_which_table VARCHAR2(15);
    lr_abc_table abc_table%ROWTYPE;
    lr_def_table def_table%ROWTYPE;
    ------ Declaration of the cursors ------
    TYPE type_ref_cur IS REF CURSOR;
    lcur_generic type_ref_cur;
    BEGIN
    IF av_input = '' THEN
    ------- Open the generic cursor ------- OPEN lcur_generic FOR
    SELECT *
    FROM abc_table
    WHERE <<fill if any conditions>>;
    lv_which_table := 'abc_table';
    ELSE
    ------- Open the generic cursor ------- OPEN lcur_generic FOR
    SELECT *
    FROM def_table
    WHERE <<fill if any conditions>>;
    lv_which_table := 'def_table';
    END IF;
    ---- Fetch the values from the cursor ----
    LOOP
    IF lv_whic_table = 'abc_table' THEN
    FETCH lcur_generic INTO lr_abc_table;
    ELSE
    FETCH lcur_generic INTO lr_def_table;
    END IF;
    EXIT WHEN lcur_generic%NOTFOUND;
    --%%% Your Business logic goes here %%%
    END LOOP;
    ----- Close the Generic cursor ------
    CLOSE lcur_generic;
    END proc_xyz;
    [E]Originally posted by Vladimir Sheftelyevich ([email protected]):
    I was hoping to find help with coding a dynamically defined cursor statement, as some of the tables in the 'FROM' clause of the cursor definition are not known until runtime. Thanks in advance for your help!
    Cheers,
    Vladimir
    null

  • Fetching values of Dynamic Cursor

    Hi,
    Can I fetch values of dynamic cursor into a variable(record OR individual)? If yes what will be the type of that record/variable?
    Thanks in Advance!
    Regards
    RK

    bluefrog wrote:
    There is no way in 10g to identify the column data types of the result set of a dynamic ref cursor.
    In 11g one can use ;
    DBMS_SQL.DESCRIBE_COLUMNS (
    c IN INTEGER,
    col_cnt OUT INTEGER,
    desc_t OUT DESC_TAB);when using DBMS_SQL
    P;In 11g you can use a ref cursor and then use the DBMS_SQL package to convert that ref cursor to a DBMS_SQL cursor where you can then describe the columns. The initial query doesn't have to be a DBMS_SQL cursor. In 10g, unfortunately, there is no way to convert the ref cursor to a DBMS_SQL cursor, so dynamic cursors would have to be written as DBMS_SQL cursors initially. Ref cursors are only really suitable for 3rd party tools, such as when passing queries back to .NET or Java applications. Too many people try and use ref cursors inside PL/SQL and then discover why that's not a good idea.
    PL/SQL 101 : Understanding Ref Cursors:-
    PL/SQL 101 : Understanding Ref Cursors

  • Reports On Ref Cursor - Patch download and Installation issue

    I have made a dummy report based on ref cursor.
    Code
    DB
    ==
    create or replace PACKAGE my_cursors IS
    type emp_rc_strong is ref cursor return emp%rowtype;
    type emp_rc_weak is ref cursor;
    function fn_RefCur(p_deptno number) return my_cursors.emp_rc_strong;
    END;
    create or replace PACKAGE body my_cursors IS
    function fn_RefCur(p_deptno number) return my_cursors.emp_rc_strong is
    emp_data_strong my_cursors.emp_rc_strong;
    emp_data_weak my_cursors.emp_rc_weak;
    begin
    open emp_data_weak for 'select * from emp where deptno = :1' using p_deptno;
    emp_data_strong := emp_data_weak;
    return emp_data_strong;
    end;
    Report Builder
    =========
    Inside ref_cursor query object
    function QR_1RefCurDS return my_cursors.emp_rc_strong is
    emp_data_weak my_cursors.emp_rc_weak;
    begin
    emp_data_strong := my_cursors.fn_RefCur(:p_deptno);
    return emp_data_strong;
    end;
    While running it's giving following error
    REP-0065: Virtual Memory System Error
    REP-0200: Cannot allocate enough memory cavaa 22
    So, when I searched on net, I found
    Cause
    This is a known Oracle Reports bug for MS Windows Platform.
    Bug 5584001 DYNAMIC REF CURSORS DON'T WORK IN VERSION 10.1.2.0.2 REPORTS BUILDER
    Solution
    To implement the solution, two one-off patches should be applied to
    Oracle Reports %ORACLE_HOME% Please execute the following steps:
    1. Download the following two one-off patches from Metalink:
    One-off Patch 4505133
    One-off Patch 5634746
    But since I am a home user, so how I can download these patches and test.

    You can't without metalink access.

  • Ref Cursor Problem in Oracle Forms 6i

    Hi all.
    In Oracle Forms 6i, can not I do this?
    DECLARE
    TYPE CUR_REF IS REF CURSOR;
    CUR_PRODUCTS CUR_REF;
    VAR_QUERY VARCHAR2(3000) := 'SELECT something FROM table';
    BEGIN
    OPEN CUR_PRODUCTS FOR VAR_QUERY;
    END;
    It generates error on opening that it expects a SELECT statement.
    How can I open a dynamic ref cursor in Oracle Forms 6i??? The same thing works fine in PL/SQL.
    Regards,
    Zaaf
    Thanks in advance.

    It could be like this:
    PROCEDURE dynamic_cursor ( PC$Clause  IN Varchar2 )
    IS
      cursor_number EXEC_SQL.CursType;
      -- Variables for the data to be returned into
      LC$ACTYPE       Number;
      -- Control variables
      LN$count       Number;
      LC$sql_order   Varchar2(256);
    BEGIN
         -- Open the cursor --
         cursor_number := Exec_SQL.Open_cursor;
         -- build the complete SQL order -- 
         LC$sql_order := PC$clause  ;
         -- Parse the SQL order --
         EXEC_SQL.PARSE(cursor_number, LC$sql_order);
         -- Define the columns for the data to be returned --
         EXEC_SQL.DEFINE_COLUMN(cursor_number,1,LC$ACTYPE);
         -- Execute the Cursor --
         LN$count := EXEC_SQL.EXECUTE(cursor_number);
         -- Loop and fetch each row from the result set --
         While EXEC_SQL.FETCH_ROWS(cursor_number) > 0 Loop
           EXEC_SQL.COLUMN_VALUE(cursor_number,1,LC$ACTYPE);
              begin
                select chqfac, pasfac, indbcr ,
                staxfl, excise, incchg, cat001 , trntyp , subtyp
                into s_chkbok, s_pasbok, s_intber,
                s_staxfl, s_excise, s_incchg, s_cat001 , ss_trntyp , ss_subtyp
                from stfeacty
                where brancd = s_brancd
                and actype = LC$ACTYPE;
                s_actype := LC$ACTYPE ;
              exception
                when no_data_found then
                  NULL;
                when too_many_rows then
                  NULL;
              end;
         End Loop ;
         -- Close the cursors
         EXEC_SQL.CLOSE_CURSOR(cursor_number);
    EXCEPTION
      When EXEC_SQL.INVALID_CONNECTION Then
         message('Unexpected Invalid Connection error from EXEC_SQL');
      When EXEC_SQL.PACKAGE_ERROR Then
         message('Unexpected error from EXEC_SQL: '||to_char(EXEC_SQL.LAST_ERROR_CODE)|| EXEC_SQL.LAST_ERROR_MESG);
      If EXEC_SQL.IS_OPEN(cursor_number) Then
        EXEC_SQL.CLOSE_CURSOR(cursor_number);
        message('Exception - Cleaned up Cursor');
      End if;
    END; That you could call with the following:
    dynamic_cursor('SELECT ac_type FROM SYSADM.SAVINGS');But, if the query is as simple with only the table name change, you could aslo use a simpliest solution based on a record group.
    Francois

Maybe you are looking for