CURSOR_ALREADY_OPEN and SYS_REFCURSOR

Hi,
I have noticed that you can open a sys_refcursor without closing it. A co-worker did it in a function and I complained it was not good practice and it would raise an error, but he told me I was wrong. I have tested it and it has worked, so I have lost the argument.
And then I found nothing on Oracle literature about it. Did I miss something? Is it common knowledge?
Thanks, Roger

And then I found nothing on Oracle literature about it. Did I miss something? Is it common knowledge? It is documented behaviour: Opening a Cursor Variable:
You need not close a cursor variable before reopening it. Note that consecutive OPENs of a static cursor raise the predefined exception CURSOR_ALREADY_OPEN. When you reopen a cursor variable for a different query, the previous query is lost.
«

Similar Messages

  • Calling a stored procedure with RAW and SYS_REFCURSOR

    How do you call a stored procedure with the following input and output parameters?
    create or replace PROCEDURE test
    v_col1 IN NUMBER DEFAULT NULL ,
    v_col2 IN VARCHAR2 DEFAULT NULL ,
    v_col3 IN RAW DEFAULT NULL ,
    v_vol4 IN DATE DEFAULT NULL,
    cv_1 IN OUT SYS_REFCURSOR
    OPEN cv_1 FOR
    SELECT
    lv_tmp1 aaaa ,
    lv_tmp2 bbbb,
    lv_tmp3 cccc
    FROM DUAL ;
    END;
    Edited by: 925963 on Apr 6, 2012 10:50 AM

    Did you try just declaring the vars?
    untested
    declare
      myCur SYS_REFCURSOR;
      myRaw RAW(4);
      BEGIN
        test (0, 0, myRaw, sysdate, myCur);
      END;

  • How to pass values and see outout of PL/SQL Tables, SYS_REFCURSORs?

    I am new to SQL Developer (Version 3.0.02).
    Our QC division wants to test stored procedures using SQL Developer.
    I tried to test procedures with PL/SQL tables and SYS_REFCURSORS as input and output parameters.
    When we run it by pressing the green arrow we get the Run PL/SQL window.
    How can we:
    - Put values to SYS_REFCURSOR and PL/SQL table type input parameters?
    - How can we display SYS_REFCURSOR and PL/SQL table type output parameters?
    All this time we were using SQL*Navigator. In Navigator the PL Tables and SYS_REFCURSORs outputs are shown in a nice grid.
    Here, in SQL Developer, do we have to WRITE our own code to input and display these types of parameters????

    Hi Channa,
    Yes in SQL Developer 3.0 you will have to write your own code to test out ref cursors.
    I had created test harness scripts for all my procedures having ref cursors as parameters.
    And I had to dbms_output all the values on the screen.
    Probably a limitation of the SQL Developer 3.0 tool.
    In case you come across different solution to this do let me know also.
    Thanks,
    Viju
    blog: http://whizdba.wordpress.com

  • Ref cursor vs sys_refcursor

    What is the diffeerenece between ref cursor and sys_refcursor.in which scenarios we use ref cusor and sys_refcursor with example.For the performace point of view which is better.

    BluShadow wrote:
    SAMBIT RAY wrote:
    Hi,
    thanks for use ful difference.
    i got your difference between two but as per performace level which is better.plz suggestAs billy said they are all cursors.
    As my link highlights, a ref cursor of any sort is just a pointer to a query.
    SYS_REFCURSOR is just a synonym for the REF CURSOR type.
    There is no performance difference.Putting on my nit picking hat :-)
    Sys_refcursor is a named type of type refcursor. The $ORACLE_HOME/rdbms/admin/stdspec.sql ther is this line:
    /* Adding a generic weak ref cursor type */
    type sys_refcursor is ref cursor;Since it is in package standard we can use it without qualifying it with a package name, but
    declare
       l_cur sys_refcursor;is exactly the same as:
    reate package my_pack as
       type john_cursor is ref cursor;
    end;
    declare
       l_cur my_pack.john_cursor;Taking off my nit picking hat now.
    John

  • Cursor difference (REF Cursor & Sys_Refcursor)

    Hi,
    Is there any difference between REF Cursor and SYS_Refcursor?
    I see some packages using sys_refcursor and some using REF Cursor.
    Package 1 (Spec):
       PROCEDURE s_demo1 (
          ret_rset   OUT      sys_refcursor
    Package 2 (Spec):
    TYPE cur_set IS REF CURSOR;
    PROCEDURE s_test1 (out_data OUT cur_set);
    PROCEDURE s_test2 (out_data OUT cur_set);I am going through this link (http://sql-plsql.blogspot.com/2007/05/oracle-plsql-ref-cursors.html) but didn't find the answer.

    DomBrooks wrote:
    John - don't get me wrong, I'm not suggesting that as soon as something like SYS_REFCURSOR comes along, everything should get rewritten. It's just I still see new packages written on 11g by, say mainly C# or java developers, which use this old style.
    But.... are you saying that you can't do this?
    CREATE OR REPLACE PACKAGE old_package AS
    TYPE pkg_cur IS REF CURSOR;
    FUNCTION foo(p_id IN NUMBER) RETURN sys_refcursor;
    END;
    CREATE OR REPLACE PACKAGE BODY old_package AS
    FUNCTION foo(p_id IN NUMBER) RETURN sys_refcursor IS
    l_cur sys_refcursor;
    BEGIN
    OPEN l_CUR FOR SELECT * FROM dual WHERE 1 = p_id;
    RETURN l_cur;
    END;
    END;
    Absolutely, I can even declare pkg_cursor as a sys_refcursor and all of the old calls will still work since sys_refcursor is just a ref cursor in disguise. What I cannot do is drop the declaration in the package spec since the outside callers rely on it, so since sys_refcusor is just a ref cursor in disguise, why use it?
    In fact, in OHOME/rdbms/admin/stdspec.sql which is the declarations for the package standard sys_refcursor is declared as:
      /* Adding a generic weak ref cursor type */
      type sys_refcursor is ref cursor;Since things declared in standard package (like data types, operators, all of the named exceptions etc.) are "globally" available without using the package name, sys_refcursor just provides a method to save a few keystrokes everywhere. It is no different functionally than a package declared ref cursor. The main difference is that you cannot create a strongly typed sys_refcursor, if you want/need one, you need to use a ref cursor.
    I do tend to use sys_refcursor in new code, but it took me a while to switch because there is not much real benefit as far as I can see.
    John
    Edited by: John Spencer on Nov 17, 2010 4:11 PM

  • Sys_refcursor and Global Temp Table

    I have a stored proc that returns a sys_refcursor that is a query from the Global Temporary Table. The problem is that I cannot get ODP.Net to return the results. It comes back empty every time.
    The GTT is set for preserve on commit but ultimately I want to have it set to delete.
    Does anyone have any experience with this and is willing to share some sample code for getting the values back from the stored proc?

    Where did you populate the GTT from? Was it from the same ODP session, using the same OracleConnection? GTT data is only visible to the session that inserted it.
    A kinda goofy thing about using GTT with an app that utilizes connection pooling is when you put a connection back in the pool and then call con.Open again
    a) you dont know for sure what connection you're getting.
    b) the connection you got from the pool could also have leftover state, as it does not get "cleaned up" between going in and coming out, and is not restored to pristine condition. If you have PLSQL package state, GTT with on commit preserve, alter session, it's possible two con.Open calls could result in differing behavior.
    I assume if you use a single Oracle connection to populate the table, then use it to call a proc to open a ref cursor from it, that works?
    comments/corrections welcome.
    Greg

  • Store and restore a sys_refcursor

    Hi
    I'm building a simple reporting system, and need to cache SQL results. As of the current implementation I use .NET to get a dataset and store that dataset to the database to be able to restore it later.
    I wondered if there is a generic way to take any sys_refcursor, store it as XML(or another format) and at a later time, restore it as sys_refcursor again.
    It would be nice to schedule the reporting and store the results without including .NET in the picture

    Maybe this gives you an idea:
    SQL> declare
      2   cur sys_refcursor;
      3   c integer;
      4   x xmltype;
      5  begin
      6   open cur for select empno, ename from emp;
      7   c := dbms_xmlgen.newcontext(cur);
      8   x := dbms_xmlgen.getxmltype(c);
      9   dbms_output.put_line(x.extract('ROWSET/ROW').getstringval());
    10   dbms_xmlgen.restartquery(c);
    11
    12   -- do something with the cursor ....
    13   close cur;
    14  end;
    15  /
    <ROW>
      <EMPNO>7369</EMPNO>
      <ENAME>SMITH</ENAME>
    </ROW>
    <ROW>
    <EMPNO>7499</EMPNO>
      <ENAME>ALLEN</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7521</EMPNO>
    <ENAME>WARD</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7566</EMPNO>
    <ENAME>JONES</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7654</EMPNO>
    <ENAME>MARTIN</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7698</EMPNO>
    <ENAME>BLAKE</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7782</EMPNO>
    <ENAME>CLARK</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7788</EMPNO>
    <ENAME>SCOTT</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7839</EMPNO>
    <ENAME>KING</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7844</EMPNO>
    <ENAME>TURNER</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7876</EMPNO>
    <ENAME>ADAMS</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7900</EMPNO>
    <ENAME>JAMES</ENAME>
    </ROW>
    <ROW>
      <EMPNO>7902</EMPNO>

  • WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR

    SYS_REFCURSOR is a Weakly Defined Cursor
    DECLARE emp_refcur SYS_REFCURSOR;
    when this is so simple
    why this
    DECLARE TYPE emp_cur_type IS REF CURSOR RETURN emp%ROWTYPE; my_rec emp_cur_type; ?
    Can anyone explain in what situations we should prefer among these ..when both can give same result set .

    > WHEN TO USE SYS_REFCURSOR AND WHEN NOT TO USE REFCURSOR
    When the requirements define that one is better suited than the other.
    There are no hard and fast rules about when to use a specific tool. There are however basic concepts of what the purpose of the tool is, and what typical problems the tool can address.
    Ref cursors in PL/SQL.. usually not the best of ideas. Ref cursors are designed for client-server processing.
    1) The client makes a request for data/information.
    2) PL applies the back-end server logic and construct a SQL statement for the client.
    3) PL passes the SQL to the SQL engine where it is parsed and a SQL cursor created.
    4) PL passes a reference handle to SQL cursor back to the client
    5) The client can now fetch the rows output from the cursor.
    Advantages.
    - The client does not need to know SQL, db structures, business logic, etc
    - The PL code can be improved, modified, new business logic added, without even touching a single byte of client code
    So what are the advantages where the "client" in the above is another PL/SQL program? None really.
    A "real" client (an external one like Java, C#, Delphi, etc) can receive the ref cursor handle and dynamically at run-time determine the projection (output) of the cursor.
    PL code cannot (unless using the latest 11g features). In PL code, a ref cursor is actually implemented as a DBMS_SQL cursor. A DBMS_SQL cursor is to PL code what ref cursor is to Java/C#/etc.
    So whether you define a strongly typed or weakly typed ref cursor in PL/SQL code... typical response is "so what?" as ref cursors are not really intended for PL code. Not that there are exceptions - they do come in handy at times.. but as exceptions to the rule.
    So my response is "so what?" - it is more important to *correctly" chose what type of client cursor data type you want to use to interact with the back-end SQL cursor. Once that decision has been made (based on technical sound logic), then one can look at  (minor) issues such as weak vs. strong ref cursors.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Can somebody explain difference between SYS_REFCURSOR and REFCURSOR

    I want to know the exact difference between sys_refcursor and refcursor.

    Please do not post Re: SYS_REFCURSOR and CURSOR. It makes them soggy and difficult to light, er, hard to follow.
    Thank you for your future co-operation.
    Cheers, APC

  • Is there any provision to view the selected record using SYS_REFCURSOR?

    hi friends ,
    I was using SQL Server . now i am shifting to Oracle . so we are changing the Stored Procedures in SQLServer to Oracle SP's. I have given the structure of procedure given below . If possible , i want to see the output of select statement in the TOAD editor . If any body knows please help me
    CREATE OR REPLACE PROCEDURE PS_AON
    P_STATUS OUT VARCHAR2,
    P_CUR OUT SYS_REFCURSOR
    AS
    BEGIN
    OPEN P_CUR FOR
              select colum1,column2,column3 from Table 1;
    EXCEPTION
                   WHEN OTHERS THEN
                   P_STATUS:=SQLERRM;
    END;
    This is one of the model of stored procedures i am using . And the editor i am using is TOAD 7.3.0 and oracle 9i. Is there any provision to view the selected records by running this procedure in TOAD editor
    thanks & regards

    (assuming you have relatively recent version of TOAD).
    Write a small block to call the procedure (or use Toad's 'execute procedure' option) as in the example below. Note the ':' in front of 'v_cur_out'. When you run the block, TOAD will prompt you for a value / datatype for 'v_cur_out'. Ignore the value, set the datatype to 'Cursor' and click OK. The resultset (if any) will be displayed in the Data Grid window below.
    DECLARE
       v_status VARCHAR2 (32767);
    BEGIN
       ps_aon (v_status, :v_cur_out);
       DBMS_OUTPUT.PUT_LINE ('v_status => ' || v_status);
    END;
    /

  • Ref Cursor and For Loop

    The query below will return values in the form of
    bu     seq     eligible
    22     2345     Y
    22     2345     N
    22     1288     N
    22     1458     Y
    22     1458     N
    22     1234     Y
    22     1333     N
    What I am trying to accomplish is to loop through the records returned.
    for each seq if there is a 'N' in the eligible column return no record for that seq
    eg seq 2345 has 'Y' and 'N' thus no record should be returned.
    seq 1234 has only a 'Y' then return the record
    seq 1333 has 'N' so return no record.
    How would I accomplish this with a ref Cursor and pass the values to the front end application.
    Procedure InvalidNOs(io_CURSOR OUT T_CURSOR)
         IS
              v_CURSOR T_CURSOR;
         BEGIN
    OPEN v_CURSOR FOR
    '     select bu, seq, eligible ' ||
    '     from (select bu, seq, po, tunit, tdollar,eligible,max(eligible) over () re ' ||
    '          from (select bu, seq, po, tunit, tdollar,eligible ' ||
    '          from ( ' ||
    '          select bu, seq, po, tunit, tdollar, eligible, sum(qty) qty, sum(price*qty) dollars ' ||
    '               from ' ||
    '               ( select /*+ use_nl(t,h,d,s) */ ' ||
    '               h.business_unit_id bu, h.edi_sequence_id seq, d.edi_det_sequ_id dseq, ' ||
    '                    s.edi_size_sequ_id sseq, h.po_number po, h.total_unit tUnit, h.total_amount tDollar, ' ||
    '                    s.quantity qty, s.unit_price price,' ||
    '               (select (case when count(*) = 0 then ''Y'' else ''N'' end) ' ||
    '          from sewn.NT_edii_po_det_error ' ||
    '          where edi_det_sequ_id = d.edi_det_sequ_id ' ||
    '               ) eligible ' ||
    '     from sewn.nt_edii_purchase_size s, sewn.nt_edii_purchase_det d, ' ||
    '     sewn.nt_edii_purchase_hdr h, sewn.nt_edii_param_temp t ' ||
    '     where h.business_unit_id = t.business_unit_id ' ||
    '     and h.edi_sequence_id = t.edi_sequence_id ' ||
    '     and h.business_unit_id = d.business_unit_id ' ||
    '     and h.edi_sequence_id = d.edi_sequence_id ' ||
    '     and d.business_unit_id = s.business_unit_id ' ||
    '     and d.edi_sequence_id = s.edi_sequence_id ' ||
    '     and d.edi_det_sequ_id = s.edi_det_sequ_id ' ||
    '     ) group by bu, seq, po, tunit, tdollar, eligible ' ||
    '     ) ' ||
    '     group by bu, seq, po, tunit, tdollar, eligible)) ';
              io_CURSOR := v_CURSOR;
    END     InvalidNOs;

    One remark why you should not use the assignment between ref cursor
    variables.
    (I remembered I saw already such thing in your code).
    Technically you can do it but it does not make sense and it can confuse your results.
    In the opposite to usual variables, when your assignment copies value
    from one variable to another, cursor variables are pointers to the memory.
    Because of this when you assign one cursor variable to another you just
    duplicate memory pointers. You don't copy result sets. What you do for
    one pointer is that you do for another and vice versa. They are the same.
    I think the below example is self-explained:
    SQL> /* usual variables */
    SQL> declare
      2   a number;
      3   b number;
      4  begin
      5   a := 1;
      6   b := a;
      7   a := a + 1;
      8   dbms_output.put_line('a = ' || a);
      9   dbms_output.put_line('b = ' || b);
    10  end;
    11  /
    a = 2
    b = 1
    PL/SQL procedure successfully completed.
    SQL> /* cursor variables */
    SQL> declare
      2   a sys_refcursor;
      3   b sys_refcursor;
      4  begin
      5   open a for select empno from emp;
      6   b := a;
      7   close b;
      8 
      9   /* next action is impossible - cursor already closed */
    10   /* a and b are the same ! */
    11   close a;
    12  end;
    13  /
    declare
    ERROR at line 1:
    ORA-01001: invalid cursor
    ORA-06512: at line 11
    SQL> declare
      2   a sys_refcursor;
      3   b sys_refcursor;
      4   vempno emp.empno%type;
      5 
      6  begin
      7   open a for select empno from emp;
      8   b := a;
      9 
    10   /* Fetch first row from a */
    11   fetch a into vempno;
    12   dbms_output.put_line(vempno);
    13 
    14   /* Fetch from b gives us SECOND row, not first -
    15      a and b are the SAME */
    16 
    17   fetch b into vempno;
    18   dbms_output.put_line(vempno);
    19 
    20 
    21  end;
    22  /
    7369
    7499
    PL/SQL procedure successfully completed.Rgds.
    Message was edited by:
    dnikiforov

  • Replacing null values in optional prompts and passing to stored proc

    Hi,
    I want to create a stored procedure with OPTIONAL prompts. When user does not pass a value for that parameter, I want to set the value for that parameter by selecting from a column in table then I WANT to use that paramvalue in sql within cursor of stored proc. How do I do that?
    In short, I want to do the following Here is the psuedocode:
    Create or replace procedure test (param IN varchar2 DEFAULT NULL)
    As
    var_param varchar(20);
    select param into var_param from dual;
    If param is null then select custid from table1 else var_param
    OPen ref_cursor for
    Select xyz from table2
    where fyy = var_para
    Can someone let me know the syntax on how to do this in stored proc?
    Regards,
    hena
    Edited by: 904385 on Dec 25, 2011 7:04 AM

    Hi,
    Merry Christmas, and welcome to the forum!
    Here's one way to do what you requested:
    CREATE OR REPLACE PROCEDURE     test
    (    param     IN     VARCHAR2     DEFAULT     NULL
    AS
        ref_cursor     SYS_REFCURSOR;
        var_param     VARCHAR2 (20)     := param;
    BEGIN
        IF  var_param  IS NULL
        THEN
         SELECT     custid
         INTO     var_param
         FROM     table1
    --     WHERE     ...     -- Unless table1 has only 1 row
        END IF;
        OPEN  ref_cursor
        FOR   SELECT  xyz
           FROM       table2
           WHERE       fyy     = var_param;
    END     test;Whatever you're trying to do, this is probably niot the simplest or most efficient way to do it.

  • Order of words, fuzzy and utl_match

    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
    PL/SQL Release 10.2.0.1.0 - Production
    "CORE     10.2.0.1.0     Production"
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    create table category(cat_id number(20),cat_type varchar2(3000));
    create table category_match(cat_id number(20),cat_type varchar2(3000));
    Insert into category (CAT_ID,CAT_TYPE) values (12790,'AUTO CONSULTANTS');
    INSERT INTO CATEGORY (CAT_ID,CAT_TYPE) VALUES (23803,'AUTO CONSULTANT');
    Insert into category (CAT_ID,CAT_TYPE) values (23804,'CONSULTANT FOR AUTO FINANCE');
    Insert into category_match (CAT_ID,CAT_TYPE) values (12790,'AUTO CONSULTANTS');
    INSERT INTO CATEGORY_match (CAT_ID,CAT_TYPE) VALUES (23803,'AUTO CONSULTANT');
    Insert into category_match (CAT_ID,CAT_TYPE) values (23804,'CONSULTANT FOR AUTO FINANCE');
    CREATE INDEX "LOOKING4"."MYINDEX" ON "CATEGORY_MATCH"
        "CAT_TYPE"
      INDEXTYPE IS "CTXSYS"."CONTEXT" ;
    CREATE INDEX "LOOKING4"."CAT_TYPE_IDX" ON "CATEGORY"
        "CAT_TYPE"
      INDEXTYPE IS "CTXSYS"."CTXCAT" ;
    select cat_id,CAT_TYPE,UTL_MATCH.edit_distance_similarity(CAT_TYPE,'AUTO CONSULTANT') from
    select * from category where catsearch(cat_type,
    '<query>
          <textquery grammar="context">
          <progression>
          <seq>auto consultant</seq>
          <seq>?(auto) and ?(consultant)</seq>
            </progression>
            </textquery>
    </query>'
    ,NULL)>0
    )where rownum<5
    23803     AUTO CONSULTANT     100
    12790     AUTO CONSULTANTS     94
    23804     CONSULTANT FOR AUTO FINANCE     26
    update category set cat_type='CONSULTANTS AUTO' WHERE CAT_ID=12790
    select cat_id,CAT_TYPE,UTL_MATCH.edit_distance_similarity(CAT_TYPE,'AUTO CONSULTANT') from
    select * from category where catsearch(cat_type,
    '<query>
          <textquery grammar="context">
          <progression>
          <seq>auto consultant</seq>
          <seq>?(auto) and ?(consultant)</seq>
            </progression>
            </textquery>
    </query>'
    ,NULL)>0
    )where rownum<5
    23803     AUTO CONSULTANT     100
    12790     CONSULTANTS AUTO     32
    23804     CONSULTANT FOR AUTO FINANCE     26
    select score(1),cat_id,cat_type from CATEGORY_MATCH where cat_id in(
    select cat_id from category where catsearch(cat_type,
    '<query>
          <textquery grammar="context">
          <progression>
          <seq>auto consultant</seq>
          <seq>?(auto) and ?(consultant)</seq>
            </progression>
            </textquery>
    </query>'
    ,NULL)>0) AND
    contains(cat_type,'?(auto) and ?(consultant)',1)>0
    9     23803     AUTO CONSULTANT
    9     12790     AUTO CONSULTANTS
    9     23804     CONSULTANT FOR AUTO FINANCEi have been using catsearch to use progressive relaxation
    there are many "cat_types" like "cat_id" =23803,12790 ,the order of words in a sentence changes
    there are upto 10 words in each row of "cat_types" column
    among others i have referred
    Achieving functionality of many preferences using one context index
    and
    Re: Fuzzy search - more accurate score??
    there is very less possibility of repetition of words in a row
    utl match seems to work perfect only when the order of appearance of words is same
    if you can suggest a way to get a very close score for cat_id 23803 and 12790 it would be much appreciated
    thanks and regards

    select *
        FROM   (SELECT score(1),score(2),score(3),score(4),GREATEST (SCORE(1), SCORE(2) - 1, SCORE(3) - 2, SCORE(4) - 3) g_scores,
                      UTL_MATCH.EDIT_DISTANCE_SIMILARITY (CAT_TYPE,'AUTO CONSULTANT') EDS,
                      CAT_ID, CAT_TYPE
                  FROM   category_match
                  WHERE  CONTAINS (cat_type, 'solar water heater* 10 * 10', 1) > 0
                  OR     CONTAINS (cat_type, 'NEAR ((?solar, ?water ,?heater), 0, TRUE) * 10 * 10', 2) > 0
                 OR     CONTAINS (cat_type, 'NEAR ((?solar, ?water ,?heater), 0, FALSE) * 10 * 10', 3) > 0
                 or     CONTAINS (CAT_TYPE, '(?solar AND ?water AND ?heater) * 10 * 10', 4) > 0
                 order  by g_scores desc, EDS desc)
       WHERE  ROWNUM<100
    100     100     100     100     100     23     4                  SOLAR WATER HEATER-ANU
    100     100     100     100     100     22     26901          SOLAR WATER HEATER SUDARSHAN SAUR
    100     100     100     100     100     21     30                  SOLAR WATER HEATER INDUSTRIAL
    100     100     100     100     100     20     17379          SOLAR WATER HEATER DEALERS-TATA
    100     100     100     100     100     20     26906          SOLAR WATER HEATER NUETECH
    100     100     100     100     100     20     11465          SOLAR WATER HEATER DEALERS-ANU
    100     100     100     100     100     20     21                  SOLAR WATER HEATER-ZING TATA BP
    100     100     100     100     100     20     11463          SOLAR WATER HEATER MANUFACTURERS-ANU
    100     100     100     100     100     19     8                  SOLAR WATER HEATER MANUFACTURERS
    100     100     100     100     100     19     23                  SOLAR WATER HEATER EVACUATED TUBE
    100     100     100     100     100     19     49                  SOLAR WATER HEATER-HOTMAX NOVA TATA BP
    100     100     100     100     100     19     13357          SOLAR WATER HEATER INDUSTRIAL DEALERS
    100     100     100     100     100     18     16300          SOLAR WATER HEATER-TECHNOMAX
    100     100     100     100     100     18     9                  SOLAR WATER HEATER DEALERS-TATA BP
    100     100     100     100     100     18     20                  SOLAR WATER HEATER-ZING
    100     100     100     100     100     18     18                  SOLAR WATER HEATER-ORB SOLAR
    100     100     100     100     100     18     22552          SOLAR WATER HEATER-KOTAK URJA
    100     100     100     100     100     18     26908          SOLAR WATER HEATER SUPREME
    100     100     100     100     100     17     26907          SOLAR WATER HEATER TECHNOMAX"
    100     100     100     100     100     17     13322          SOLAR WATER HEATER DISTRIBUTORS
    100     100     100     100     100     17     22                  SOLAR WATER HEATER-ETC TATA BP
    100     100     100     100     100     17     48                  SOLAR WATER HEATER-VAJRA PLUS TATA BP
    100     100     100     100     100     17     27084          SOLAR WATER HEATER SALES
    100     100     100     100     100     16     16236          SOLAR WATER HEATER DEALERS-RACOLD
    100     100     100     100     100     16     15                  SOLAR WATER HEATER-NUTECH
    100     100     100     100     100     16     1                  SOLAR WATER HEATER DEALERS
    100     100     100     100     100     15     2                  SOLAR WATER HEATER DEALERS-TATA BP SOLAR
    100     100     100     100     100     15     31                  SOLAR WATER HEATER DOMESTIC
    100     100     100     100     100     15     13                  SOLAR WATER HEATER DEALERS-V GUARD
    100     100     100     100     100     14     17                  SOLAR WATER HEATER-KAMAL SOLAR
    100     100     100     100     100     13     11467          SOLAR WATER HEATER DEALERS-GILMA
    100     100     100     100     100     13     19                  SOLAR WATER HEATER-GILMA
    100     100     100     100     100     13     10                  SOLAR WATER HEATER REPAIRS & SERVICES-TATA SOLAR
    100     100     100     100     100     12     10578          SOLAR WATER HEATER
    100     100     100     100     100     11     3                  SOLAR WATER HEATER REPAIRS & SERVICES
    0      0     100     100     98     25     10120          WATER HEATER SOLAR INDUSTRIAL
    0      0     100     100     98     20     12953          WATER HEATER SOLAR-RACOLD
    0      0     100     100     98     17     10119          WATER HEATER SOLAR RESIDENCIAL
    {code}
    the query is working accurately technically
    but is there any way to get 10578 on top
    the requirement is
    ---first
    solar water heater
    solar water heater dealers
    solar water heater manufacturers
    solar water heater distributors
    solar water heater sales
    solar water heater repairs and servicing
    ---followed by
    SOLAR WATER HEATER REPAIRS & SERVICES-TATA SOLAR
    SOLAR WATER HEATER-KAMAL SOLAR
    SOLAR WATER HEATER DEALERS-TATA BP SOLAR   etc etc
    so if the end user types in "solar water" the top row would have a row from the table that has what the end user has entered followed by "dealers" or "manufacturers" or "distributors" or "sales" or "repairs and servicing"
    so if a row contains "solar water dealer" it shows up on top
    or(if "solar water dealer" is not there and "solar water manufacturers" or  "solar water distributors" etc is not present)
    a row from the table that has what the end user has entered PLUS "heater" followed by "dealers" or "manufacturers" or "distributors" or "sales" or "repairs and servicing"
    so "solar water heater dealers" shows up on top
    these words - "dealers" , "manufacturers" , "distributors" , "sales" , "repairs and servicing"  etc remain constant
    what i am using right now is
    {code}
    create or replace
    procedure HOME_OLD
    p_cat_type in varchar2,
    P_LOC IN NUMBER,
    P_MAX IN NUMBER,
    P_MIN IN NUMBER,
    P_OUT OUT SYS_REFCURSOR
    as
    VARIAB varchar2(500);
    VARIAB2 varchar2(500);
    VARIAB3 varchar2(500);
    VARIAB4 varchar2(500);
    begin
    --VARIAB2:='?'||replace(P_CAT_TYPE,' ',', ?');
    --VARIAB3:='?'||replace(P_CAT_TYPE,' ',' ?');
    --DBMS_OUTPUT.PUT_LINE(VARIAB2);
    --DBMS_OUTPUT.PUT_LINE(VARIAB3);
    SELECT stragg(cat_id) into variab
       FROM   (SELECT GREATEST (SCORE(1), SCORE(2) - 1, SCORE(3) - 2, SCORE(4) - 3) score,
                                      CAT_ID, CAT_TYPE
                  FROM   category_match
                  -- exact words in order:
                  WHERE  CONTAINS (cat_type,get_basic(P_CAT_TYPE), 1) > 0
                  -- similar words next to each other in order:
                  OR     CONTAINS (cat_type, get_near_syntax(P_CAT_TYPE), 2) > 0
                 -- similar words next to each other in any order:
                 OR     CONTAINS (cat_type, get_near_syntax_desc(P_CAT_TYPE), 3) > 0
                 -- similar words anywhere in any order:
                 OR     CONTAINS (cat_type, get_anywhere(P_CAT_TYPE), 4) > 0
                 order  by score desc)
       where  rownum < 3;
    DBMS_OUTPUT.PUT_LINE(VARIAB);
    open p_out
       FOR select * from(select rownum r,name,address1,telephone,mobile,CAT_TYP,cat_id,
    (case when  address2=p_loc and ACT_STATUS='Y'  then '1' when  address2=p_loc  then '2' when address2 in
    (select NEARBY_LOC from NEAR_BY where LOCALITY_ID=p_loc) and ACT_STATUS='Y' 
    then '3' when ADDRESS2 in (select NEARBY_LOC from NEAR_BY where LOCALITY_ID=p_loc)
    then '4' when ACT_STATUS='Y' and address2<> p_loc then '5' else '6' end) as marker
      FROM TEST_TEST
      WHERE
      CAT_ID in(select * from table(STRING_TO_TABLE_NUM(variab))) and rownum<P_MAX order by marker) where r>P_MIN;
    IF VARIAB IS NULL THEN
    OPEN P_OUT
       FOR SELECT * FROM(SELECT rownum r,name,address1,telephone,mobile,CATS
       FROM   (SELECT GREATEST (SCORE(1), SCORE(2) - 1, SCORE(3) - 2, SCORE(4) - 3) score,
                                      NAME,ADDRESS1,TELEPHONE,MOBILE,CATS
                  FROM   TEST_TEST2
                  -- exact words in order:
                  WHERE  CONTAINS (NAME,get_basic(P_CAT_TYPE), 1) > 0
                  -- similar words next to each other in order:
                  OR     CONTAINS (NAME, get_near_syntax(P_CAT_TYPE), 2) > 0
                 -- similar words next to each other in any order:
                 OR     CONTAINS (NAME, get_near_syntax_desc(P_CAT_TYPE), 3) > 0
                 -- similar words anywhere in any order:
                 OR     CONTAINS (NAME, get_anywhere(P_CAT_TYPE), 4) > 0
                 ORDER  BY SCORE DESC)
       WHERE  ROWNUM < P_MAX)where r>P_MIN;
    END IF;
    end home_old;
    {code}
    the flow is to find what the end user has entered in category table ,if a match exists,find all reg_ids from test_test materialized view that have selected the matched cat_id..
    the test_test materialized view lists each company cat_id-selected-by-that-company number of times 
    if no match is found in category table what the end user has entered could be a company so  a search in name column of test_test2 materialized view..
    this materialized view has one entry for each  company
    {code}
    create or replace
    FUNCTION GET_BASIC(P_CAT_TYPE VARCHAR2)
        RETURN VARCHAR2
      is
      VARIAB2 VARCHAR2(3000);
          begin
    VARIAB2:='{'||P_CAT_TYPE||'}*10*10'; 
    return(VARIAB2);
    END;
    create or replace
    FUNCTION GET_NEAR_SYNTAX(P_CAT_TYPE VARCHAR2)
        RETURN VARCHAR2
      is
      VARIAB2 VARCHAR2(3000);
          begin
    VARIAB2:='NEAR((?{'||replace(P_CAT_TYPE,' ','}, ?{')||'}),10,TRUE)*10*10'; 
    return(VARIAB2);
    END;
    create or replace
    FUNCTION GET_NEAR_SYNTAX_DESC(P_CAT_TYPE VARCHAR2)
        RETURN VARCHAR2
      is
      VARIAB2 VARCHAR2(3000);
          begin
    VARIAB2:='NEAR((?{'||replace(P_CAT_TYPE,' ','}, ?{')||'}),10,FALSE)*10*10'; 
    return(VARIAB2);
    END;
    {code}
    can anything be done to ameliorate this whole flow
    can anything be done to eliminate the near_by and act_status and locality checking in ordering by "marker" clause
    below is the materialized view creation ddl
    SELECT IN_V.REG_ID,
        IN_V.NAME,
        IN_V.TELEPHONE,
        IN_V.MOBILE,
        IN_V.ADDRESS1,
        IN_V.ADDRESS2,
        IN_V.ACT_STATUS,
        resec.cat_id,
        UPPER(STRAGG(IN_V.CAT_TYPE)) AS cat_typ
      FROM
        (SELECT RSC.REG_ID,
          R.NAME,
          RSC.CAT_ID,
          C.CAT_TYPE,
          R.ADDRESS1,
          R.ADDRESS2,
          R.ACT_STATUS,
          R.TELEPHONE,
          R.MOBILE,
          ROW_NUMBER() OVER (PARTITION BY RSC.REG_ID ORDER BY rsc.reg_id) AS TT
        FROM REG_SEG_CAT RSC,
          category C,
          REGISTRATION R
        WHERE C.CAT_ID=RSC.CAT_ID
        AND R.REG_ID  =RSC.REG_ID
        ) IN_V,
        REG_SEG_CAT RESEC
      WHERE in_v.reg_id=resec.reg_id
      AND IN_V.TT      <6
      GROUP BY IN_V.REG_ID,
        IN_V.NAME,
        IN_V.TELEPHONE,
        IN_V.MOBILE,
        IN_V.ADDRESS2,
        IN_V.ACT_STATUS,
        IN_V.ADDRESS1,
        resec.cat_id;
      and sql>desc test_test
    REG_ID
    NAME
    TELEPHONE
    MOBILE
    ADDRESS1
    ADDRESS2
    ACT_STATUS
    CAT_ID
    CAT_TYP
    please let me know if you need more info
    Edited by: 946207 on Apr 19, 2013 6:22 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • What's the best way to create and free temporaries for CLOB parameters?

    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production on Solaris
    I have a procedure calling another procedure with one of the parameters being an IN OUT NOCOPY CLOB.
    I create the temporary CLOB in proc_A, do the call to proc_B and then free the temporary again.
    In proc_B I create a REFCURSOR, and use that with dbms_xmlgen to create XML.
    So the code basically looks like
    CREATE OR REPLACE PROCEDURE client_xml( p_client_id IN            NUMBER
                                           ,p_clob      IN OUT NOCOPY CLOB   ) AS
       v_rc         SYS_REFCURSOR;
       v_queryCtx   dbms_xmlquery.ctxType;
    BEGIN
       OPEN c_rc FOR
          SELECT col1
                ,col2
                ,col3
            FROM clients
           WHERE client_id = p_client_id;
       v_queryCtx := dbms_xmlgen.newContext(v_rc);
       p_clob     := dbms_xmlgen.getXML(v_queryCtx, 0);
    END;
    CREATE OR REPLACE PROCEDURE my_proc AS
       v_clob       CLOB;
       v_client_id  NUMBER;
    BEGIN
       v_client_id := 123456;
       dbms_lob.createTemporary(v_clob, TRUE, dbms_lob.CALL);
       client_xml( p_client_id => v_client_id
                  ,p_clob      => v_clob);
       dbms_lob.freeTemporary(v_clob);
    END;However, I just learned the hard way that IN OUT NOCOPY is only a hint, and that Oracle sometimes creates a local variable for the CLOB anyway.
    A solution is to change the client_xml procedure above to
    CREATE OR REPLACE PROCEDURE client_xml( p_client_id IN            NUMBER
                                           ,p_clob      IN OUT NOCOPY CLOB   ) AS
       v_rc         SYS_REFCURSOR;
       v_queryCtx   dbms_xmlquery.ctxType;
    BEGIN
       IF NOT NVL(dbms_lob.istemporary(p_clob),0) = 1 THEN
          dbms_lob.createTemporary(p_clob, TRUE, dbms_lob.CALL);
       END IF;
       OPEN c_rc FOR
          SELECT col1
                ,col2
                ,col3
            FROM clients
           WHERE client_id = p_client_id;
       v_queryCtx := dbms_xmlgen.newContext(p_refcursor);
       p_clob     := dbms_xmlgen.getXML(v_queryCtx, 0);
    END;My concern is that in case Oracle does create a local variable, 2 temporaries will be created, but there will only be 1 freeTemporary.
    Could this lead to a memory leak?
    Or should I be safe with the solution above because I'm using dbms_lob.CALL?
    Thanks,
    Arnold
    Edited by: Arnold vK on Jan 24, 2012 11:52 AM

    Arnold vK wrote:
    However, I just learned the hard way that IN OUT NOCOPY is only a hint, and that Oracle sometimes creates a local variable for the CLOB anyway.A CLOB variable in called a locator. Just another term for a pointer.
    A CLOB does not exist in local stack space. The variable itself can be TBs in size (max CLOB size is 128TB depending on DB config) - and impossible to create and maintain in the stack. Thus it does not exist in the stack - and is why the PL/SQL CLOB variable is called a locator as it only contains the pointer/address of the CLOB.
    The CLOB itself exists in the database's temporary tablespace - and temporary LOB resource footprint in the database can be viewed via the v$temporary_lobs virtual performance view.
    Passing a CLOB pointer by reference (pointer to a pointer) does not make any sense (as would be the case if the NOCOPY clause was honoured by PL/SQL for a CLOB parameter). It is passed by value instead.
    So when you call a procedure and pass it a CLOB locator, that procedure will be dereferencing that pointer (via DBMS_LOB for example) in order to access its contents.
    Quote from Oracle® Database SecureFiles and Large Objects Developer's Guide
    >
    A LOB instance has a locator and a value. The LOB locator is a reference to where the LOB value is physically stored. The LOB value is the data stored in the LOB.
    When you use a LOB in an operation such as passing a LOB as a parameter, you are actually passing a LOB locator. For the most part, you can work with a LOB instance in your application without being concerned with the semantics of LOB locators. There is no requirement to dereference LOB locators, as is required with pointers in some programming languages.
    >
    The thing to guard against is not freeing CLOBs - the age old issue of not freeing pointers and releasing malloc'ed memory when done. In PL/SQL, there is fairly tight resource protection with the PL/SQL engine automatically releasing local resources when those go out of scope. But a CLOB (like a ref cursor) is not really a local resource. And as in most other programming language, the explicit release/freeing of such a resource is recommended.

  • How to get UTF-8 encoding when create XML using DBMS_XMLGEN and UTL_FILE ?

    How to get UTF-8 encoding when create XML using DBMS_XMLGEN and UTL_FILE ?
    Hi,
    I do generate XML-Files by using DBMS_XMLGEN with output by UTL_FILE
    but it seems, the xml-Datafile I get on end is not really UTF-8 encoding
    ( f.ex. cannot verifying it correct in xmlspy )
    my dbms is
    NLS_CHARACTERSET          = WE8MSWIN1252
    NLS_NCHAR_CHARACTERSET     = AL16UTF16
    NLS_RDBMS_VERSION     = 10.2.0.1.0
    I do generate it in this matter :
    declare
    xmldoc CLOB;
    ctx number ;
    utl_file.file_type;
    begin
    -- generate fom xml-view :
    ctx := DBMS_XMLGEN.newContext('select xml from xml_View');
    DBMS_XMLGEN.setRowSetTag(ctx, null);
    DBMS_XMLGEN.setRowTag(ctx, null );
    DBMS_XMLGEN.SETCONVERTSPECIALCHARS(ctx,TRUE);
    -- create xml-file:
    xmldoc := DBMS_XMLGEN.getXML(ctx);
    -- put data to host-file:
    vblob_len := DBMS_LOB.getlength(xmldoc);
    DBMS_LOB.READ (xmldoc, vblob_len, 1, vBuffer);
    bHandle := utl_file.fopen(vPATH,vFileName,'W',32767);
    UTL_FILE.put_line(bHandle, vbuffer, FALSE);
    UTL_FILE.fclose(bHandle);
    end ;
    maybe while work UTL_FILE there is a change the encoding ?
    How can this solved ?
    Thank you
    Norbert
    Edited by: astramare on Feb 11, 2009 12:39 PM with database charsets

    Marco,
    I tryed to work with dbms_xslprocessor.clob2file,
    that works good,
    but what is in this matter with encoding UTF-8 ?
    in my understandig, the xmltyp created should be UTF8 (16),
    but when open the xml-file in xmlSpy as UTF-8,
    it is not well ( german caracter like Ä, Ö .. ):
    my dbms is
    NLS_CHARACTERSET = WE8MSWIN1252
    NLS_NCHAR_CHARACTERSET = AL16UTF16
    NLS_RDBMS_VERSION = 10.2.0.1.0
    -- test:
    create table nh_test ( s0 number, s1 varchar2(20) ) ;
    insert into nh_test (select 1,'hallo' from dual );
    insert into nh_test (select 2,'straße' from dual );
    insert into nh_test (select 3,'mäckie' from dual );
    insert into nh_test (select 4,'euro_€' from dual );
    commit;
    select * from nh_test ;
    S0     S1
    1     hallo
    1     hallo
    2     straße
    3     mäckie
    4     euro_€
    declare
    rc sys_refcursor;
    begin
    open rc FOR SELECT * FROM ( SELECT s0,s1 from nh_test );
    dbms_xslprocessor.clob2file( xmltype( rc ).getclobval( ) , 'XML_EXPORT_DIR','my_xml_file.xml');
    end;
    ( its the same when using output with DBMS_XMLDOM.WRITETOFILE )
    open in xmlSpy is:
    <?xml version="1.0"?>
    <ROWSET>
    <ROW>
    <S0>1</S0>
    <S1>hallo</S1>
    </ROW>
    <ROW>
    <S0>2</S0>
    <S1>straޥ</S1>
    </ROW>
    <ROW>
    <S0>3</S0>
    <S1>m㢫ie</S1>
    </ROW>
    <ROW>
    <S0>4</S0>
    <S1>euro_€</S1>
    </ROW>
    </ROWSET>
    regards
    Norbert

Maybe you are looking for

  • Console Problems

    Recently I had to open my console program was opened when I was uninstalling a program. However, now when I open it it give the following over and over: Looking for devices matching vendor ID=1193 and product ID=8718 I'm guessing this is not normal.

  • Could not initiate the BPEL process because the input xml is not well forme

    Your test request generated the following exception/fault: Could not initiate the BPEL process because the input xml is not well formed, the reason is : Error parsing envelope: (2, 152) Invalid char in text. Please correct the input xml. <soap:Envelo

  • Comment connecter un picoprojecteur sur un macbook pro ?

    Bonjour, j ai un macbook pro et un pico projecteur samsung, apres avoir consulté un vendeur F**C j' ai acheté un câbleMini Display Port to VGA Adapter J' ai une image ( le fond d' écran ) mais pas ce que j' ai sur l' écran de mon mac, Quand je l' all

  • Music from CD get messed up

    I am working on a project that was created by someone else on another computer. Some of the music that was imported from a CD seems to get screwed up. I would spent time placing music exactly where I want it, then later I would go back to the project

  • NML2NDeviceObserver pop up after trying to install Telecom tstick HELP

    Hi. I was using The Vodafone VODEM but decided to change to the Telecom Tstick. I installed it but now get this infuriating pop up that will not go away no matter what! Tried shutting down, searching for files related to it and deleting them, and rea