Order by clause in cursor problem

Hello,
I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
Sample code:
CREATE PACKAGE Announces AS
TYPE tRefCur IS REF CURSOR;
PROCEDURE TopAnnounces(
iiCount IN NUMBER,
osAnnounces OUT tRefCur,
oiRetVal OUT NUMBER
END Announces;
CREATE PACKAGE BODY Announces AS
PROCEDURE TopAnnounces(
iiCount IN NUMBER,
osAnnounces OUT NUMBER,
oiRetVal OUT NUMBER
AS
BEGIN
OPEN osAnnounces FOR
SELECT Id, Name, AnnCount FROM
SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
GROUP BY Id, Name
-- bellow is the line, where the code crash
ORDER BY AnnCount DESC
WHERE ROWNUM < iiCount + 1;
oiRetVal := 0;
EXCEPTION
WHEN OTHERS THEN
oiRetVal := -255;
END TopAnnounces;
END Announces;
If I compile the code above I will get this error:
PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
After I remark the problematic line, the compilation is successful (but not the result :).
Is there something I'm doing wrong?
Thanks for advice
Vojtech Novacek
null

Sorry you can not use order by clause into one temporal table created by subquery.
Put the order by clause offside of subquery.
Atte.
CC.
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Vojtech Novacek:
Hello,
I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
Sample code:
CREATE PACKAGE Announces AS
TYPE tRefCur IS REF CURSOR;
PROCEDURE TopAnnounces(
iiCount IN NUMBER,
osAnnounces OUT tRefCur,
oiRetVal OUT NUMBER
END Announces;
CREATE PACKAGE BODY Announces AS
PROCEDURE TopAnnounces(
iiCount IN NUMBER,
osAnnounces OUT NUMBER,
oiRetVal OUT NUMBER
AS
BEGIN
OPEN osAnnounces FOR
SELECT Id, Name, AnnCount FROM
SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
GROUP BY Id, Name
-- bellow is the line, where the code crash
ORDER BY AnnCount DESC
WHERE ROWNUM < iiCount + 1;
oiRetVal := 0;
EXCEPTION
WHEN OTHERS THEN
oiRetVal := -255;
END TopAnnounces;
END Announces;
If I compile the code above I will get this error:
PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
After I remark the problematic line, the compilation is successful (but not the result :).
Is there something I'm doing wrong?
Thanks for advice
Vojtech Novacek<HR></BLOCKQUOTE>
null

Similar Messages

  • Passing parameters in order by clause of cursor

    Hi friends,
    I am facing a strange problem
    I have a cursor with a order by clause.
    OPEN cursor_nomrpt FOR
    SELECT DISTINCT
    CAST(plano.desc3 AS VARCHAR2(50)) category
    ,plano.name plan_name
    ,nvl(CAST(plano.desc6 AS VARCHAR2(50)),'not applicable') pcrr
    ,nvl(trunc(plano.value18,2),0) nominal_slm
    ,nvl(trunc(plano.value19,2),0) blm
    ,plano.dbdateeffectivefrom date_live
    plano.dbkey pog_id
    ,case when plano.value47 < 0 THEN 0 when plano.value47 IS NULL THEN 0 ELSE plano.value47 END AS parent_id
    FROM
    dplapro1.ix_spc_planogram plano
    INNER JOIN dplapro1.ix_spc_performance perf ON plano.dbkey = perf.dbparentplanogramkey
    INNER JOIN dplapro1.ix_spc_product product ON perf.dbparentproductkey = product.dbkey
    INNER JOIN dplapro1.ix_spc_product_key prodkey ON product.dbkey2 = prodkey.dbkey
    AND prodkey.keylevel = 2
    WHERE
    plano.value50 = 0
    AND plano.dbkey4 = p_subcatkey
    ORDER BY ltrim(rtrim(p_orderby)) ASC;
    p_orderby is the parameter being passed. But the output is not sorted. But when I run the cursor by hardcoding the parameter value it works fine...
    Need your help on this

    Hi,
    When you use a local variable in a cursor, it's as if you had hard-coded a literal in its place, so you can't use a variable for a column name.
    If you know all the possible values of p_orderby, you can do something like this:
    ORDER BY  TRIM ( CASE  p_orderby
                   WHEN  'DESC'     THEN  plano.desc
                   WHEN  'VALUE'     THEN  plano.value
               END
                ) ASC;If you don't know all the possible values, you could use dynamic SQL.
    By the way,
    TRIM (x)returns the same results as
    LTRIM ( RTRIM (x))

  • Form4.5 For update clause in cursor problem

    Hi,
    I am putting this code in my forms4.5 pl/sql block and get the error when compile the pl/sql block.
    "Encountered ";" when expecting "of".....
    Code is:
    Select null INTO dummy from mytable
    where id = 1456
    for update nowait;
    If I remove for update clause it compiles ok.
    So can anyone tell me what's going on here...
    thx

    Actually I used...
    For update of column_name and it worked.
    I have another problem which I am posting here in a new thread.
    thx

  • Order by clause in PL/SQL cursors

    I am trying to execute a procedure with some input parameters. I open a cursor
    with a select statement. However, the order by clause in the query does not
    recognize parameter sent through the procedure input parameters.
    For example:
    open <<cursor name>> for
    select id from member order by <<dynamic parameter>>" does not work (Compiles fine but does not return the right result).
    But if I try and give a static order by <<column name>> it works. Is the
    order by clause in the PL/SQL a compile time phenomenon?
    I have also tried it through dynamic sql. All the other parameters work except the order by <<parameter>> asc|desc
    Also "asc" and "desc" does not work if given dynamically.
    What alternatives do I have?
    null

    I don't think order by can be dynamic in a cursor, but it sure can be using dynamic sql. The only issue is that you must do a replace in the sql string with the dynamic variable. For example:
    create or replace procedure test_dyn(p_col in varchar2, p_order in varchar2) as
    q varchar2(500);
    u_exec_cur number;
    u_columnnumber NUMBER;
    u_columndate DATE;
    u_columnvarchar varchar2(50);
    u_cur_count number;
    u_ename varchar2(20);
    u_sal number;
    begin
    q := 'select ename, sal from scott.emp order by p_col p_order';
    -- got to do these two replaces
    q:= replace(q,'p_col',p_col);
    q:= replace(q,'p_order',p_order);
    u_exec_cur := dbms_sql.open_cursor;
    dbms_sql.parse(u_exec_cur,q,dbms_sql.v7);
    dbms_sql.define_column(u_exec_cur, 1, u_columnvarchar, 20);
    dbms_sql.define_column(u_exec_cur, 2, u_columnnumber);
    u_cur_count := dbms_sql.execute(u_exec_cur);
    loop
    exit when (dbms_sql.fetch_rows(u_exec_cur) <= 0);
    dbms_sql.column_value(u_exec_cur, 1, u_ename);
    dbms_sql.column_value(u_exec_cur, 2, u_sal);
    dbms_output.put_line(u_ename);
    dbms_output.put_line(u_sail);
    --htp.p(u_ename);
    --htp.p(u_sal);
    end loop;
    end;
    show errors;
    Now when when I execute my procedure I can change the order by clause all I want, for example:
    SQL> set serveroutput on;
    SQL> exec gmika.test_dyn('sal','asc');
    SMITH
    800
    ADAMS
    1100
    WARD
    1250
    MARTIN
    1250
    MILLER
    1300
    TURNER
    1500
    ALLEN
    1600
    JLO
    2222
    BLAKE
    2850
    JONES
    2975
    SCOTT
    3000
    FORD
    3000
    LOKITZ
    4500
    KING
    5000
    JAMES
    5151
    JAMES
    5555
    PL/SQL procedure successfully completed.
    SQL>
    null

  • Problem in sql query because of order by clause

    Hi All,
    I am facing a one problem in my one sql query.I am using Oracle 10gR2.
    Query is given below:
    SELECT t1.ename
            FROM T1, T2
           WHERE T1.EMPNO = 1234
             AND T1.ACCOUNTNO = T2.ACCOUNTNO
             AND T1.SEQ = T2.SEQ
           ORDER BY T2.SEQThe Plan of the query is :
    | Id  | Operation                     | Name                 | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT              |                      |     2 |   218 | 11716   (1)| 00:00:41 |
    |*  1 |  TABLE ACCESS BY INDEX ROWID  | T1                   |     1 |    89 |     1   (0)| 00:00:01 |
    |   2 |   NESTED LOOPS                |                      |     2 |   218 | 11716   (1)| 00:00:41 |
    |*  3 |    TABLE ACCESS BY INDEX ROWID| T2                   |     2 |    40 | 11715   (1)| 00:00:41 |
    |   4 |     INDEX FULL SCAN           | PK_T2_SEQ            | 58752 |       |   122   (5)| 00:00:01 |
    |*  5 |    INDEX RANGE SCAN           | FK_ACCOUNTNO         |     3 |       |     0   (0)| 00:00:01 |
    ----------------------------------------------------------------------------------------------------Now i want to reduce the time of this query.
    If i am removing Order by clause from query than performance of the query is totally perfect but with order by clause its taking a time.
    I have already set SORT_AREA_SIZE but still nothing is improving.
    Welcome and thanks for your suggestions.
    Thanks,
    Edited by: BluShadow on 23-Jun-2011 07:55
    added {noformat}{noformat} tags and formatted explain plan to make it readable.  Please see {message:id=9360002} for details on how to post code and data                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Hi,
    There are a couple of things I do not understand.
    1. Why don't you put {noformat}{noformat} around your code, it makes it so much easier to read, especially your explain plan
    2. You claim that the ORDER BY is problematic compared to no order by. Then why do you choose to post only one plan?
    3. It is hard to understand how your tables relate, and which indexes you have and which you don't.
    - PK_T2_SEQ, does this mean that SEQ alone is primary key of T2?
    - If SEQ is primary key of T2, why do you join on accountno, seq and not just seq?
    - If SEQ is primary key of T2 one of the tables is denormalized.
    4. FK_ACCOUNTNO, is this an index on accountno, alone?
    - Or is this AccountNo, Seq?
    5. Is there no index on T1.EMPNO?
    Above could of course just be a case of my not understanding the names of your indexes.
    So, here are my guesses:
    Above plan is for the ORDER BY query. That means the optimizer, has chosen to full scan PK_T2_SEQ, since data is then read according to the ORDER BY.
    (This could be a bad choice)I
    You could try and order by t1.seq, instead. Result should be the same.
    Regards
    Peter                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Problem with order by clause

    Hai all,
    I have problem with order by clause,
    My query is
    "select number from table1 order by number asc "
    and the output is displaying as
    1
    10
    12
    13
    15
    17
    19
    2
    20
    21
    22
    But if we give order by it should display as below only right ?
    1
    2
    10
    12
    13
    15
    17
    19
    20
    21
    22 ........
    Please help me why it is not displaying like it. and how to make the statement to display like the second case. Thanks in advance.
    Regards,
    Uraja

    The column datatype that you are selecting is not of NUMBER datatype(might be char or varchar2) hence you are getting such result set.
    And for this purpose, it is recommended to set datatype of a column appropriately.
    For now you can add TO_NUMBER function to column in ORDER BY clause, only if it has data of number type.
    Edited by: Ora on 19 Nov, 2012 3:10 AM

  • Cursor ORDER BY Clause Changing Row Count In BULK COLLECT ... FOR LOOP?

    Oracle 10g Enterprise Edition Release 10.2.0.4.0 running on Windows Server 2003
    Oracle Client 10.2.0.2.0 running on Windows 2000
    I have some PL/SQL code that's intended to update a column in a table based on a lookup from another table. I started out by testing it with the UPDATE statement commented out, just visually inspecting the DBMS_OUTPUT results to see if it was sane. During this testing I added/changed the cursor ORDER BY clause to make it easier to read the output, and saw some strange results. I've run the code 3 times with:
    1. no ORDER BY clause
    2. ORDER BY with two columns (neither indexed)
    3. ORDER BY with one column (not indexed)
    and get three different "rows updated" counts - in fact, when using the ORDER BY clauses it appears that the code is processing more rows than without either ORDER BY clause. I'm wondering why adding / changing an ORDER BY <non-indexed column> clause in a cursor would affect the row count?
    The code structure is:
    TYPE my_Table_t IS TABLE OF table1%ROWTYPE ;
    my_Table my_Table_t ;
    CURSOR my_Cursor IS SELECT * FROM table1 ; -- initial case - no ORDER BY clause
    -- ORDER BY table1.column1, table1.column2 ; -- neither column indexed
    -- ORDER BY table1.column2 ; -- column not indexed
    my_Loop_Count NUMBER := 0 ;
    OPEN my_Cursor ;
    LOOP
    FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
    EXIT WHEN my_Table.COUNT = 0 ;
    FOR i IN 1..my_Table.COUNT LOOP
    my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
    EXIT WHEN my_New_Value IS NULL ;
    EXIT WHEN my_New_Value = <an undesirable value> ;
    IF my_New_Value <> my_Table(i).column3 THEN
    DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
    UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
    my_Loop_Count := my_Loop_Count + 1 ;
    END IF ;
    END LOOP ;
    COMMIT ;
    END LOOP ;
    CLOSE my_Cursor ;
    DBMS_OUTPUT.PUT_LINE( 'Processed ' || my_Loop_Count || ' Rows ' ) ;

    Hello (and welcome),
    Your handling the inner cursor exit control is suspect, which will result in (seemingly) erratic record counts.
    Instead of:
    LOOP
    FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
    EXIT WHEN my_Table.COUNT = 0 ;
    FOR i IN 1..my_Table.COUNT LOOP
    my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
    EXIT WHEN my_New_Value IS NULL ;
    EXIT WHEN my_New_Value = <an undesirable value> ;
    IF my_New_Value my_Table(i).column3 THEN
    DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
    UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
    my_Loop_Count := my_Loop_Count + 1 ;
    END IF ;
    END LOOP ;
    COMMIT ;
    END LOOP ;Try this:
    LOOP
    FETCH my_Cursor BULK COLLECT INTO my_Table LIMIT 100 ;
    FOR i IN 1..my_Table.COUNT LOOP
    my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
    EXIT WHEN my_New_Value IS NULL ;
    EXIT WHEN my_New_Value = <an undesirable value> ;
    IF my_New_Value my_Table(i).column3 THEN
    DBMS_OUTPUT.PUT_LINE( 'Changing ' || my_Table(i).column3 || ' to ' || my_New_Value ) ;
    UPDATE table1 SET column3 = my_New_Value WHERE column_pk = my_Table(i).column_pk ;
    my_Loop_Count := my_Loop_Count + 1 ;
    END IF ;
    EXIT WHEN my_Cursor%NOTFOUND;
    END LOOP ;
    END LOOP ;
    COMMIT ;Which also takes the COMMIT outside of the LOOP -- try to never have a COMMIT inside of any LOOP.
    Additionally, not too sure about these:
    my_New_Value := <call a pkg.funct to retrieve expected value from another table> ;
    EXIT WHEN my_New_Value IS NULL ;
    EXIT WHEN my_New_Value = <an undesirable value> ;Any one of those EXITs will bypass your my_Loop_Count increment.
    Edited by: SeánMacGC on Jul 9, 2009 8:37 AM
    Had the cursor not found in the wrong place, now corrected.

  • Problem in a inner query- order by clause

    hi...
    I have a update statement with a simple select clause present as inner query..
    (select col1 from table1 where col2='abc' and rownum=1 order by col3 desc)
    since it is a inner query, thats why i can not remove the brackets.
    col3 may be 0 or 1(1 can occur only once...0 can be multiple times)
    my target is to fetch the record if the col3 is 1
    if it is not 1, then it will fetch the first record with col3=0...thats why i have put order by col3.
    moreover i want only one record, thats why i have put rownum=1.
    but it is failing with 'missing right parenthesis'.
    please help..

    Hi,
    Remember that the ORDER BY clause is applied last, after the WHERE clause is completed, so when you way
    WHERE     ROWNUM = 1
    ORDER BY  col3    DESCin the same sub-query, you're picking one row (arbitrarily), and then "sorting" that one row.
    Here's one way to get the results you want:
         SELECT     col1
         FROM     (
                   SELECT  col1
                   ,     ROW_NUMBER () OVER (ORDER BY  col3  DESC)
                                  AS r_num
                   FROM     table1
                   WHERE     col2     = 'abc'
                   AND     col3     IN (0, 1)
         WHERE     r_num     = 1
    )Depending on how this is used in your complete query, there may be better ways to get the same results.

  • Is parameter in ORDER BY clause possible?

    I'm using a function to return a ref cursor and currently pass a parameter without any problems. I would like to change the sort on the fly by passing a parameter to the order by clause, but Oracle ignores it.
    CREATE OR REPLACE PACKAGE pkg_agent_appt_status AS
    TYPE rcur IS REF CURSOR;
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2) RETURN rcur;
    END pkg_agent_appt_status;
    CREATE OR REPLACE PACKAGE BODY pkg_agent_appt_status AS
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2)
    RETURN rcur
    IS
    retval rcur;
    BEGIN
    OPEN retval FOR
    SELECT agncy.CORPORATE_NAME "Agency Name",
    agnt_state.APPT_STATE "State",
    agnt.AGENT_STATUS "Appt Status",
    TO_CHAR(agnt_state.APPT_STATE_EFF_DT,'mm/dd/yyyy') "Effective Date",
    agnt.AGENT_NUMBER "Agent ID",
    agnt.AGENT_STATUS "Agent Status",
    STATE.STATE_NAME
    FROM AGNT_APPT_STAT_PRDCR_WRK agnt,
    AGNT_APPT_STAT_WRK agncy,
    AGNT_APPT_STATE_STAT_PRDCR_WRK agnt_state,
    STATE
    WHERE agnt.AGENT_TAX_ID = ssn
    AND agnt.COMPANY_CODE = agncy.COMPANY_CODE
    AND agnt.PARENT_AGENT_AGENCY_ID = agncy.AGENT_NUMBER
    AND agnt.COMPANY_CODE = agnt_state.COMPANY_CODE
    AND agnt.AGENT_AGENCY_ID = agnt_state.AGENT_AGENCY_ID
    AND agnt.AGENT_NUMBER = agnt_state.AGENT_NUMBER
    AND agnt_state.APPT_STATE = STATE.STATE_CODE
    ORDER BY
    sort_str;                     
    RETURN retval;
    END f_agent_appt_status;
    END pkg_agent_appt_status;

    If you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
    create or replace someProc( someArg varchar2 )
    as
      strSQL varchar2(4000)
    begin
      strSQL := <<string containing your SQL statement up to the order by clause>>
      strSQL := strSQL || 'ORDER BY ' || someArg
      execute immediate strSQL;
    endJustin

  • Is passing parameter to ORDER BY clause possible?

    I'm using a function to return a ref cursor and currently pass a parameter without any problems. I would like to change the sort on the fly by passing a parameter to the order by clause, but Oracle ignores it.
    CREATE OR REPLACE PACKAGE pkg_agent_appt_status AS
    TYPE rcur IS REF CURSOR;
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2) RETURN rcur;
    END pkg_agent_appt_status;
    CREATE OR REPLACE PACKAGE BODY pkg_agent_appt_status AS
    FUNCTION f_agent_appt_status (ssn IN VARCHAR2, sort_str IN VARCHAR2)
    RETURN rcur
    IS
    retval rcur;
    BEGIN
    OPEN retval FOR
    SELECT agncy.CORPORATE_NAME "Agency Name",
    agnt_state.APPT_STATE "State",
    agnt.AGENT_STATUS "Appt Status",
    TO_CHAR(agnt_state.APPT_STATE_EFF_DT,'mm/dd/yyyy') "Effective Date",
    agnt.AGENT_NUMBER "Agent ID",
    agnt.AGENT_STATUS "Agent Status",
    STATE.STATE_NAME
    FROM AGNT_APPT_STAT_PRDCR_WRK agnt,
    AGNT_APPT_STAT_WRK agncy,
    AGNT_APPT_STATE_STAT_PRDCR_WRK agnt_state,
    STATE
    WHERE agnt.AGENT_TAX_ID = ssn
    AND agnt.COMPANY_CODE = agncy.COMPANY_CODE
    AND agnt.PARENT_AGENT_AGENCY_ID = agncy.AGENT_NUMBER
    AND agnt.COMPANY_CODE = agnt_state.COMPANY_CODE
    AND agnt.AGENT_AGENCY_ID = agnt_state.AGENT_AGENCY_ID
    AND agnt.AGENT_NUMBER = agnt_state.AGENT_NUMBER
    AND agnt_state.APPT_STATE = STATE.STATE_CODE
    ORDER BY
    sort_str;
    RETURN retval;
    END f_agent_appt_status;
    END pkg_agent_appt_status;

    This is quite easy, as you are already using a REF CURSOR. Instead of
    OPEN retval FOR
    SELECT ...
    ORDER BY sort_str;we can code this:
    OPEN retval FOR
    'SELECT ...
    ORDER BY '|| sort_str;Watch out for the single quotes in your query e.g. the date format mask: you need to wrap these in an additional set of single quotes i.e. 'mm/dd/yyyy' becomes ''mm/dd/yyyy''.
    Cheers, APC

  • Order by clause -dyamically in Forms

    I am using Forms 6i. I want to do Forms to Excel stuff and I am doing this by opening a cursor and then start Excel routine using OLE2.
    The user will select the 'order by' columns from a list-field in the form.
    Problem is I have given Order by clause as given below sample, but it just don't work..
    How to accomplish this ??
    declare
    Cursor emp_cursor is
    SELECT code,name,dept,desig,salary
    FROM emp
    order by :s1,:s2;
    begin
    -- excel routine
    end;

    Hi vdsk,
    Here is link to create_group_from_query built_in:
    http://www.oracle.com/webapps/online-help/forms/10g/state?navSetId=_&navId=3&vtTopicFile=f1_help/builta_c/creategp.html&vtTopicId=
    you can prepare your select statement and then concatenate with order by clause. I think that is the best soultion for you.
    Regards
    Jakub

  • ORDER BY clause in runtime

    Hi All,
    I want to order my SELECT using "dynamic ORDER BY". I have a CURSOR (in my PL/SQL procedure) contains a select with ORDER BY clause. I have implemented two combobox in my form (the first contains the column name, the second contains ASC and DESC).I tried with the next:
    1) Dynamic ORDER BY in CURSOR with combobox results. IT DOESN'T WORK.
    2) Using a block property named "ORDER BY CLAUSE". Here I put, for example, :myBlock.comboColumnOrder. IT DOESN'T WORK.
    3) Using a function: set_block_property(myBlock,order_by,:myBlock.comboColumnOrder) or set_block_property(myBlock,default_where,:myBlock.comboColumnOrder). IT DOESN'T WORK.
    4) Using dynamic CURSOR: ORACLE FORMS give me an error that said: "...it can't to the CLIENT-SIDE".
    I have thought my last solution, but it maybe cumbersome. I do a TEMPORARY TABLE, and my CURSOR insert in this TABLE, and then I'll fetch to my BLOCK.
    Thanks a lot.
    PS: My ORACLE Version is 10g.

    PROCEDURE CHEQUEAR_V0 IS
         V_ORDEN VARCHAR2(50) := '' || :BLOCK_CHECK.ORDER_BY_NAME || ' ' || :BLOCK_CHECK.ORDER_BY_ASC_DES ;
    CURSOR FILA_RESULTADO_TABLA IS
         SELECT Last_name,First_name,App_user, App_role, UserDB
         FROM adm_users
         WHERE
         ((Last_name IS NULL OR Last_name LIKE '%'||:BLOCK_CHECK.Last_name||'%') AND
         (First_name IS NULL OR First_name LIKE '%'||:BLOCK_CHECK.First_name||'%') AND
         (App_user IS NULL OR App_user LIKE '%'||:BLOCK_CHECK.App_user||'%') AND
         (App_role IS NULL OR App_role LIKE '%'||:BLOCK_CHECK.App_role||'%') AND
         (UserDB IS NULL OR UserDB LIKE '%'||:BLOCK_CHECK.User_DB||'%'))
    ORDER BY V_ORDEN;
         V_Last_name VARCHAR2(50);
         V_First_name VARCHAR2(50);
         V_App_user VARCHAR2(50);
         V_App_role VARCHAR2(50) ;
         V_User_DB VARCHAR2(50);
         CONT NUMBER ;
    BEGIN
         CONT := 1 ;
              GO_BLOCK('BLOCK_APPS_GRANTS');     
              CLEAR_BLOCK;
         SYNCHRONIZE;     
              OPEN FILA_RESULTADO_TABLA;
              LOOP
                   FETCH FILA_RESULTADO_TABLA INTO V_Last_name,V_First_name,V_App_user,V_App_role,V_User_DB ;
              EXIT WHEN FILA_RESULTADO_TABLA%NOTFOUND;
                        :BLOCK_APPS_GRANTS.Last_name := V_Last_name ;           
                        :BLOCK_APPS_GRANTS.First_name := V_First_name ;                               
                        :BLOCK_APPS_GRANTS.App_user := V_App_user ;
                        :BLOCK_APPS_GRANTS.App_role := V_App_role ;
                        :BLOCK_APPS_GRANTS.User_DB := V_User_DB ;                                         
                   NEXT_RECORD;
                   CONT := CONT+1;          
              END LOOP;
              IF FILA_RESULTADO_TABLA%ISOPEN THEN
                   CLOSE FILA_RESULTADO_TABLA;
              END IF;
              SET_BLOCK_PROPERTY('BLOCK_APPS_GRANTS', ORDER_BY, :BLOCK_CHECK.ORDER_BY_NAME || ' ' || :BLOCK_CHECK.ORDER_BY_ASC_DES);
              GO_BLOCK('BLOCK_APPS_GRANTS');
              EXECUTE_QUERY;
    synchronize;
              exception when others then
                   raise;
    END;
    Thanks a lot.
    Edited by: user11285646 on 22-jul-2009 2:30

  • ORDER BY Clause in XMLAGG is throwing errors.

    Hi,
    I'm getting errors when generating the XML using the ORDER BY clause in the XMLAgg function. Although, running the SQL directly in SQL Plus doesn't have any problem.
    Here is the PL/SQL sample.
    DECLARE
    vX_XML SYS.XMLTYPE;
    vCL_XML CLOB;
    BEGIN
    SELECT XMLElement("GROUP",
    XMLAttributes(Group_Id AS "ID", Group_Name AS "NAME", Group_Description AS "DESC"),
    XMLAgg(XMLElement("FIELD",
    XMLAttributes(Field_Id AS "ID", Field_Description AS "DESC",
    Table_Name AS "TAB_NM", Rule_Name AS "RULE_NM",
    Field_Hierarchy_Depth AS "DOL_VAR_CALC")) ORDER BY Field_Description))
    INTO vX_XML
    FROM (SELECT g.Group_Id, g.Group_Name, g.Group_Description, f.Field_Id, f.Field_Description,
    DECODE(f.Table_Name, 'ESTIMATES', 'E', 'LINE_ITEMS', 'L', NULL) Table_Name,
    rt.Rule_Name,
    CASE NVL(f.Field_Hierarchy_Depth, 0) WHEN 0 THEN 'N' ELSE 'Y' END Field_Hierarchy_Depth
    FROM GROUPS g,
    Rules r,
    Rule_Types rt,
    Fields f
    WHERE g.Group_Id = 3087
    AND r.Group_Id_Fk = g.Group_Id
    AND rt.Rule_Type_Id = r.Rule_Type_Id_Fk
    AND f.Field_Id = r.Field_Id_Fk) tab
    GROUP BY Group_Id, Group_Name, Group_Description;
    vCL_XML := vX_XML.GetCLOBVal();
    END;
    And, here are the errors.
    ERROR at line 8:
    ORA-06550: line 8, column 23:
    PLS-00306: wrong number or types of arguments in call to 'XMLAGG'
    ORA-06550: line 8, column 23:
    PL/SQL: ORA-00904: "XMLAGG": invalid identifier
    ORA-06550: line 6, column 5:
    PL/SQL: SQL Statement ignored
    Thanks in Advance.
    BTW, we are using Oracle 9.2.0.4.

    This is bug 2785463 with no ETA for a fix
    regards
    Coby D. Adams Jr.

  • Exception caused by bind variables in ORDER BY clause and VO RANGE_PAGING

    Hi,
    I'm using a ViewObject in RANGE_PAGING mode and discovered a problem when using bind variables in the ORDER BY clause of my statement:
    SELECT * FROM (SELECT /*+ FIRST_ROWS */ IQ.*, ROWNUM AS Z_R_N FROM (
      SELECT * FROM T_TABLE WHERE (attr1 = :1) ORDER BY decode (attr2, :2, 1, null, 0, -1) desc, attr3 ) IQ 
      WHERE ROWNUM < :2) WHERE Z_R_N > :3 When a bind variable is used in the ORDER BY CLAUSE and the method VO.getEstimatedRowCount() is call then the DB issued an SQL error:
    java.sql.SQLException: Ungültiger Spaltenindex (eng.: invalid column index)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:137)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:174)
         at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:239)
         at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:4612)The numbering of the bind variables indicates that the last two bind variables have the wrong numbering (??)
    When I rewrite the SQL into the following statement, the SQL error does not occur but I have not verified yet that the VO delivers the correct range of data because the numbering of the bind variables is not correct.
    SELECT * FROM (SELECT /*+ FIRST_ROWS */ IQ.*, ROWNUM AS Z_R_N FROM (
      SELECT *, decode (attr2, :1, 1, null, 0, -1) as ATTR_FOR_SORTING FROM T_TABLE WHERE (attr1 = :2) ORDER BY ATTR_FOR_SORTING  desc, attr3 ) IQ 
      WHERE ROWNUM < :2) WHERE Z_R_N > :3I'm working with JDev 10.1.2 and Oracle DB 9203.
    Any comments are welcome!
    Thanks,
    Markus

    You are using the bind variable :2 twice. Did you try
    SELECT * FROM (SELECT /*+ FIRST_ROWS */ IQ.*, ROWNUM AS Z_R_N FROM (
      SELECT * FROM T_TABLE WHERE (attr1 = :1) ORDER BY decode (attr2, :2, 1, null, 0, -1) desc, attr3 ) IQ 
      WHERE ROWNUM < :4) WHERE Z_R_N > :3
    setWhereClauseParm(0, "a"); // :1
    setWhereClauseParm(1, "b"); // :2
    setWhereClauseParm(2, "c"); // :3
    setWhereClauseParm(3, "b"); // :2setting the fourth parameter the same as the second one?
    I remeber reading something about using bind variables twice causing problems, but I#m not sure about it.

  • [JPA/TOPLINK] is the function "lower" supported in "order by" clause?

    In EJB-QL
    I can use lower() or upper() in where clause.
    But there is always a parse exception thrown when i tried to use it in main clause or order by clause.
    works:
    select s from Student s where lower(s.name) like 'm%'
    exception thrown:
    select s from Student s where s.name like 'm%' order by lower(s.name)
    OR --------------
    Why i am asking this is that the resultset returned from database is not alphabetical sorted but ascii sorted, which means any characters with upper case is always prior to the ones with lower case.
    If EJB-QL doesn't support using lower() in order by clause, do I have any other options to avoid this problem?
    BTW, it is the Oracle 10g we are using as DB
    many thanks,
    Xuphey
    Edited by: Xuphey on Nov 29, 2007 1:33 AM

    If you want to do this, you'd have to use dynamic SQL (execute immediate or DBMS_SQL). For the easier 'execute immediate' approach, you'd do something like
    create or replace someProc( someArg varchar2 )
    as
      strSQL varchar2(4000)
    begin
      strSQL := <<string containing your SQL statement up to the order by clause>>
      strSQL := strSQL || 'ORDER BY ' || someArg
      execute immediate strSQL;
    endJustin

Maybe you are looking for

  • Unlocked iPhone 3GS, Tethering and Carrier Updates.

    I originally purchased my iPhone 3GS on pay as you go locked to O2, but i got it unlocked through O2, i now use it on a variety of carriers in several countries and would like to tether it. My iPhone is currently on iOS4 and there is no option to tet

  • Indexing error in CTXSYS.INSO_FILTER

    hi, Do you know, how can i correctly creat the index in oracle text? I have created a text table: CREATE TABLE texttable(text_id int PRIMARY KEY not null, titel varchar2(500), author varchar2(20), datum date, text_size int, text_typ varchar2(10), con

  • Pre-Configure QT 7 Installation for only specific file types

    Is there an 'ini' or similar configuration file or command line option set I can use to pre-configure QT7 to only associate with specific file types? I want to leave the associations for Media Player intact and have QT handle only MOV and other Apple

  • Lock object vith forgien key

    HI All, I have 4 table that are related via forgin key ,i want to create lock object and my question is if i need to create lock object for each table or just one lock object. Best Regards Joy

  • Is there a PDF print driver for ipad

    I want to be able to print individual pages from an excel file so i can email them. So i am looking for a print driver or print program to make this happen Some of the apps i have a re polaris office, expert PDF, Adobe.