Order by clause require dynamic value
Hi
I want to bind parameters to orderby clause. In where clause with bind parameter works fine. But in orderby clause ive done similar steps to have a dynamic sort which will be passed from the previous page in a session variable. Although it didnt show any error at the backend where i can see data getting binded, but no sorting is taking place.
Can u help me to solve this.
regards
Jayashri
Jayashri,
I have tested this and you are right. And there does occur an error at the backend, only you wont see it unless you run with BC4J debug mode turned on. You can do this by providing -Djbo.debugoutput=console to the JVM. When running inside JDeveloper, you can accomplish this by going to Project Properties => PRofiles => Development => "Runner", and fill this in at the "Java Options" field.
Doing that will show the reason of the problem. When a view object is queried, BC4J actually performs 2 queries. One to obtain the 'estimated row count', and then one to obtain the action rows.
Now suppose your query looks like this:
SELECT <somefields> FROM <table>
WHERE <field> = :1 ORDER BY :2
Bc4j will construct the following query to obtain the estimated rowcount:
SELECT count(1) FROM (SELECT <somefields> FROM <table> WHERE <field> = :1)
As you can see, it does NOT include the ORDER BY of the original query in this statement, probably for performance reasons (why order the set if you are only interested in the count). Unfortunately, it WILL try to bind both :1 AND :2 to this query, but because there is only one bind variable in this query, this throws an java.sql.SQLException which is unfortunately trapped somewhere in BC4J code, and not shown in the regular log file.
I am pretty confident that this is actually a BC4J bug (or known restriction). You could post this problem at the JDeveloper Forum to determine this (without mentioning JHeadstart, because this would also happen if you wrote your own code for binding these parameters).
As for a workaround, you could try the following:
1.) Remove the (bind parameter from) the order by clause on the ViewObject
2.) Remove that same bind parameter from the "Query Bind Parameters" property.
You should now have gone back to a scenario where there are only bind parameters on the where clause. You would need to set the OrderBy clause programatically now. To do this, extend the JhsDataAction for this page, and override the method 'applyIterBindParams()'. Before calling super, try something like:
ViewObject vo = ib.getRowSetIterator().getRowSet().getViewObject();
vo.setOrderByClause(<your dynamic orderby>);
Kind regards,
Peter Ebell
JHeadstart Team
Similar Messages
-
Procedure that is Submitted as a JOB requires Dynamic Values as Input
Hello,
I am submitting a Procedure as a DBMS_JOB.SUBMIT. However, the Procedure that is being submitted requires a Dynamic Values as Input. Can we parse or construct a Procedure with Values which are dynamically constructed and then pass that to the DBMS_JOB.SUBMIT.
Can this be achieved by DBMS_SQL to parse and get the Parsed Value.
Please help.
Thanks and Regards
Shivanand KoppalkarYou can get those values from a table populated before the submit or to call a procedure with parameters from the job but running the job directly with DBMS_JOB.RUN.
Joel P�rez -
How to use "Order by" clause dynamically on LOV values in 10g r2 forms
Hi ,
I have following requirement,please guide me.
1 Create a List Of Values with 2 fields, Code and Description
2. Do not use order by clause in record Group Query
3. Attach this LOV to a field in Form
4. When user invokes the LOV user will see two fields in LOV with header as Code and Description
5. Now when user clicks on Column Header “Code” then LOV should be sorted on Code
6. And if User clicks on Column Header “Description” then LOV should be sorted on Description
Thanks in Advance.Kindly post this problem in this forum ->
[Forms Forum|http://forums.oracle.com/forums/forum.jspa?forumID=82]
And, close this thread by marked it as answered. ;)
Regards.
Satyaki De. -
Sorting character column ( used in order by clause dynamically)
Hi,
I need help on sorting character-based numbers. Like say, I want to sort customers based on street numbers(which is a character string being used in the
order by clause) they live in.
The criteria are :
i. Numbers must take precedence.
This being a character string, 1000001 comes before 2. This shouldn't happen. And you cannot use to_number
since using it with a string having characters in it would raise an error.
ii. If only a single alphabet occurs as the last character, then treat the whole string as a number except the last character and then sort it
as if sorting a number. Something like : if you have 1000A, 200D, 200B, 1000X, the result would be 200B,200D,1000A,1000X.
iii. if a character occurs elsewhere in the string, then perform the search normally as if performing a character search.
The output of the following data :
100
A101
B100A
110C
C120B
120
100020
120C
C1100
100D
would be like :
100
100D
110C
120
120C
100020
A101
B100A
C120B
C1100
Please note that the sort is being done dynamically, so I could have access to the values of the street numbers only during run time.
Any help is really appreciated.
Thanks in advance.
Regards,
Anil.Create a function to test whether the column is numeric :
create FUNCTION is_numeric(v_number VARCHAR2)
RETURN INTEGER
IS
l_number NUMBER;
BEGIN
IF INSTR(UPPER(v_number),'E') > 0 THEN
RETURN 0;
END IF;
l_number := TO_NUMBER(v_number);
RETURN 1;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
And try this query
Assume the table name is TEST with column STREET
select * from TEST
order by case is_numeric(STREET) when 1 then LPAD(STREET,20, ' ') else STREET end
Please make sure that 20 mentioned above is the column size for STREET.
Hope this helps.
-Nags -
Order by clause based on parameter value
hi,
i am using reports 6i with 10 g db
I have to create a report sorted in order of four fields. there is a parameter for selecting which field should come first in order by clause.
ie the user has provision to view the report in selected sorted order
suppose the four fields are emp_id, name, dept_id, desig .
if the user select dept_id , then the order by clause should be in order of dept_id,emp_id,name,desig
Please help to solve this scenario.
ThanksHi Rinz,
Use the query as follows :
select column_name1, column_name1, ....
from table_name
&order_by_clauseNow in the AFTER-PARAMETER-FORM trigger set the order by clause as below depending upon requirements :
if :sort_column = dept_id then
:order_by_clause := 'order by dept_id,emp_id,name,desig';
else
:order_by_clause := 'order by emp_id, dept_id, name,desig';
end if;
Here sort_column is User parameter which will be selected by user, provide list of Values for it.
Hope this helps
Best Regards
Arif Khadas -
I need to dynamically build the order by clause of my query.
I tried this:
<named-query name="list">
<query>
<![CDATA[
SELECT p FROM Person p
ORDER BY :orderby
]]>
</query>
</named-query>
@SuppressWarnings("unchecked")
public List<Person> list(String sort) {
Query query = getEntityManager().createNamedQuery("list");
query.setParameter("orderby", sort);
return query.getResultList();
}But at runtime it throws an exception:
com.microsoft.sqlserver.jdbc.SQLServerException:
L'elemento SELECT identificato da ORDER BY 1 include una variabile nell'espressione che identifica la posizione di una colonna.
Le variabili sono consentite solo nell'ordinamento in base a un'espressione che fa riferimento a un nome di colonna.The translation of the italian message is something like:
The SELECT element identified by ORDER BY 1 includes a variable that identifies the position of a column.
The valid variables in the order by clause must refer to the name of a columnThe value of the parameter sort is_ the name of a column!
Any hint?My solution was to append the order clause to an "namedQuery" like this:
- declare the named query as usual, with annotation
- create a simple helper function like findNamedQuery to find the query string for a given class and queryName (using reflection)
- if you need to execute the query without sort order - use EntityManager.createNamedQuery (as usual)
- BUT: if you want to sort data: use EntityManager.createQuery with the string obtained from findNamedQuery and the sort clause
I guess the is a performance penalty, but.. .it works. -
How to make an ORDER BY clause dynamic
is there anyway to make the ORDER BY clause in an SQL query within a DB control
dynamic? I have tried to pass a String of the column name and place it in the
statement within {}, but it doesn't work"Mark" <[email protected]> wrote:
>
is there anyway to make the ORDER BY clause in an SQL query within a
DB control
dynamic? I have tried to pass a String of the column name and place
it in the
statement within {}, but it doesn't workDid you find how ? please let me know, I also need to have a dynamic order by
clause. -
Order By Clause with Empty Field values !
Hello,
In Oracle , the order by clause does not return the expected query result, if any of the field value in the order by clause has an empty string.
Oracle treats the empty string as null value and ORDER BY gives a result with the empty string field values listed at last.
For example :
test is a sample table containing "name" field.
Query: "select name from test order by name"
In SQL Server/Access
Result (1)
NAME
(blank)
(blank)
User1
User2
User3
In Oracle
Result (2)
NAME
User1
User2
User3
(blank)
(blank)
I know some of the Work arounds for this as listed below :
1) To use NVL in Order By Clause.
i.e., the modified query
"select name from test order by nvl(name,0)"
gives the result same as Result (1).
2) To have single blank space in the field value if it is empty.
I dont want to use either of these two options b'se it would lead to a mass change in my existing code.
Is there any way i can do a
collation order settings in the Oracle databases to get the results as in MS SQL/Access.
Can Any help me out in Solving this?
Thanks
Devi Shankar
nullBharath,
I am moving this question to the SQL forum.
Regards,
Geoff -
Order by clause Dynamic in Oracle function
How can i get order by Clause Dynamic in Oracle function
My function Returns sql query with SYS_REFCURSOR . and i will pass the order by column as input parameter
create or replace
FUNCTION TEST_SSK
p_srot number
RETURN SYS_REFCURSOR
AS
C_testssk SYS_REFCURSOR;
BEGIN
OPEN C_TESTSSK FOR
SELECT LOAN_CODE,LOAN_DATE,DUE_DATE,LOAN_AMT FROM LOAN_MASTER
order by P_SROT;
return C_testssk;
end;Edited by: user10736825 on Dec 22, 2010 11:34 AMyou can go for a dynamic query ;)
create or replace
FUNCTION TEST_SSK
p_srot number
RETURN SYS_REFCURSOR
AS
C_testssk SYS_REFCURSOR;
l_str VARCHAR2(4000);
l_order VARCHAR2(100);
BEGIN
l_str := 'SELECT LOAN_CODE,LOAN_DATE,DUE_DATE,LOAN_AMT FROM LOAN_MASTER ';
IF p_sort = 'LC'
THEN
l_order := ' ORDER BY LOAN_CODE ';
ELSIF p_sort = 'LD'
THEN
l_order := ' ORDER BY LOAN_DATE ';
END IF;
l_str := l_str || l_order ;
OPEN C_TESTSSK FOR l_str;
return C_testssk;
end; -
Can i assign an 'order by' clause dynamically in forms ??
I know it's possible to assign an 'order by' clause in reports with lexical parameter.
for example..
select A
from TABLE
where A is not null
&V_ORDERBY
In this, v_orderby might be 'order by name' like that,,,
can i assign an 'order by' clause dynamically IN FORMS ??
If you understan my question, please answer to me,,,ㅜㅜHave you tried this build-in function?
SET_BLOCK_PROPERTY('[BLOCK_NAME]', ORDER_BY, 'SORTCOL1, SORTCOL2...');
Where 'SORTCOL1, SORTCOL2...' are the table columns name. -
Dynamic ORDER BY clause - Possible different data types...
I have a lexical parameter in my ORDER BY clause:
ORDER BY &P_ORDER_BY
My problem is that I have to designate this parameter as either a date, character or number, but the user defined column being ordered by could be any one of these. When I designate the parameter as a character type, and a number data type column is chosen, it orders by that value, but it orders it as though it were a character field, referencing the initial digit and not the overall numeric value. If I choose a number parameter type, and a character value is chosen, I get an invalid number error, and so on.
Anyone have any ideas how I can do this?Okay, now we're on the right track. The lexical parameter I'm using allows the user to choose from a list of the columns being retrieved in the SELECT statement, so it will always be one that's in there, as you said. Now, since I don't know which one it's going to be, I can't just make all but one of them 'None' for the break order, BUT, what I intended to do was to add a column to the SELECT statement that would always return the column they chose, and I could put the break order on that column, leaving all the rest 'None'. However, in the past, this has created 2 problems for me.
1.) As I mentioned in the first post, the column being returned could have a data type of character, number or date, but the parameter must be designated as only one of these. So, if the user chooses to order by a column having a value with a data type other than what the parameter is designated to be, then I get errors, it doesn't order correctly, etc...
2.) This is the strange, but anytime I've marked all but one column as 'None' for the Break Order, my child data doesn't always stay with the parent. For example, the report is on an IT request database. The 'group fields' consist of data elements that are demographic info.of a request (requester, date requested, description, etc...), and the details for each request list out the comments that have been made in the system for that request. The problem I'm seeing when I set the break orders this way is that each request displays not only its own comments, but it lists out comments for other requests as well, and there doesn't seem to be any logic to which request's comments it lists in addition to its own, it just lists as many as will fit on the page...I don't know if I explained that very well, but that's what's happening.
Does any of that make sense? Any ideas? -
Why order by clause maintains column number values instead of column names
why order by clause maintains column number values instead of column names ?
we can use oder by 1,2 as column number
UGNo one said that it can't be used. What's your point?
To OP: It can be written with the column's 'select list positional number' just because for the support of laziness. :)
There's no difference between 'ORDER BY name, address' and 'ORDER BY 1,2'. -
Hi all,
I'm creating a webpage using jsp thats accessing a mysql database. When I get a ResultSet from a query, I was wondering if anyone could give me some pointers on how I could sort the results based on a field name that someone clicks on. For example:
Column1 Column2
5 2
8 1
3 6I'd like to create hyperlinks on the field names so when a user clicks on it, it will sort the data in according to that field. Here is my code that I am trying to modify:
public String displayResultSet(ResultSet rs)
{// return a result set as an html table
String htmlTable = "";
try {
// get the number of columns
ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
// create a table and display the mysql field names as table headers
htmlTable += "<TABLE>\n<TR>\n";
for(int i=1; i<=numColumns; i++)
htmlTable += "<TH>" + rsmd.getColumnName(i) + "</TH>\n";
htmlTable += "</TR>\n\n";
// create an object to temporarily hold a piece of ResultSet data
Object cellData = null;
// display the data in the ResultSet in rows of an html table
while(rs.next())
{// while the ResultSet has more rows, convert them to html
htmlTable += "<TR>";
for(int j=1; j<=numColumns; j++)
{// if the data is not null, display it, otherwise display a blank space
cellData = rs.getObject(j);
if(cellData != null)
htmlTable += "<TD>" + rs.getObject(j) + "</TD>\n";
else
htmlTable += "<TD> </TD>\n";
htmlTable += "</TR>\n\n";
htmlTable += "</TABLE>\n\n";
catch (Exception e)
{ return("Error accessing database: " + e); }
return htmlTable;
}Any help or references to where I can read up on it would be appreciated. Thanks
LeonYour display code should not be mixed in with your database code. That functionality should be seperated classes.
Generally GUIs do the ordering themselves. They do not repeatedly call the database.
But if you want you pass in a descriptor that defines the fields that will be ordered on. You optionally map that to the exact database field names. You then use StringBuilder to construct the entire SQL string and add the order by clause dynamically at the end. Then you execute it. -
Dynamic where clause for MULTIPLE values
In the following of dynamic where clause?
i have some somewhat different problem.
Is a dynamic where clause for multiple values possible?
If the inputvariable varCode1,varCode2,varCode3,varCode4 are 0 then show all id's otherwise filter the resultset on var1 and/or var2,and/or var3,and/or var4.
Example table:
create table t
(d int,var int);
insert into t values (1 ,1 );
insert into t values (2 ,1 );
insert into t values (3 ,2 );
insert into t values (4 ,3 );
insert into t values (5 ,4 );
insert into t values (6 ,4 );
insert into t values (7 ,4 );
insert into t values (8 ,4 );
insert into t values (9 ,5 );
insert into t values (10, 6);
insert into t values (11, 6);
So what i want to change the where clause upon the value of the varCodes. If var1 and var are not 0 a "AND" should be used otherwise an "OR"
select id
from tst
where var = DECODE( :varCode1, 0, var, :varCode1)
or/and(?) DECODE( :varCode2, 0, var, :varCode2)
or DECODE( :varCode3, 0, var, :varCode3)
or DECODE( :varCode4, 0, var, :varCode4)Please turn off your Caps Lock
and try this link
Dynamic WHERE clause -
Using dynamic values in the IN clause
hey guys ,
is it possible to use dynamic values for comparing in the IN clause while writting query in views, in adf bc .Thanks to Jobinesh http://jobinesh.blogspot.com/2010/12/using-oraclejbodomainarray-with.html
Timo
Maybe you are looking for
-
My macbook pro can't find my external drive after upgrading os
I have a macbook pro from 2009. I recently upgraded to Yosemite and now it won't read my external drive (Toshiba). I am able to find it in disk utility but I am not able to open it. The device works fine in other computers. I have all my pictures the
-
Down payment Request from purchase order (me21n)
We need Down payment Request automatically created as per payment terms from purchase order screen.(ME21N)
-
Page footer information is missing for the first page alone.
Hi All, After exporting to text format, Page footer information is missing for the first page alone. Any reasons? Thanks for any help !! Rgds, Shriram
-
I was moving folders around and one got dragged off screen. I let go of the mouse and it disappeared. I tried to undo the move but the option was not available. When I go to save a bookmark, the option for the folder is still there, but in the bookma
-
I want to download the new itunes version to my PC so i can sync my ipad but friends have said that they lost their apps after doing so. Can i get the apps back that i paid for?