an SQL query [message #442275] |
Sat, 06 February 2010 07:13  |
navin_deep
Messages: 18 Registered: November 2009
|
Junior Member |
|
|
Hi
What is the use of below logic in where clause :
select max(emplid) into a from employees where 1=2;
I run it and can interpret result, since 1<>2, query will return no rows.
But still i want to know where such logic can be applied and useful also ?
Regards
-navin
|
|
|
|
Re: an SQL query [message #442299 is a reply to message #442275] |
Sat, 06 February 2010 11:49   |
navin_deep
Messages: 18 Registered: November 2009
|
Junior Member |
|
|
Hi Michel
Thanks for your prompt reply. With the output, i have more doubts now.
Why the output shows 'No rows selected' in first case and '1 row seelected' when we used max(empno)?
Also where can such logic( where 1=2 or where 1=1) be applied in any application ?
Regards
-navin
|
|
|
|
|
Re: an SQL query [message #442384 is a reply to message #442301] |
Sun, 07 February 2010 09:27   |
 |
Littlefoot
Messages: 21826 Registered: June 2005 Location: Croatia, Europe
|
Senior Member Account Moderator |
|
|
MichelQuote:Also where can such logic( where 1=2 or where 1=1) be applied in any application ?
Bad programming.
I will not doubt Michel's statement, but there are situations where such things can be used.
For example, there is a lexical parameter in Reports Builder which comes in handy when you have to dynamically create a certain WHERE condition for the SELECT statement.
Sometimes you need to select records that satisfy a condition based on input parameter; then you create a lexical parameter which looks likeif input_parameter = 'Y' then
lex_parameter := 'and cb_active = 1';
else
lex_parameter := 'and 1 = 1';
end if;
In report's query, you'd then use it asselect id, ename, address
from some_table
where hiredate >= trunc(sysdate, 'yyyy')
&lex_parameter which, depending upon input parameter, becomes eitherwhere hiredate >= trunc(sysdate, 'yyyy')
and cb_active = 1 orwhere hiredate >= trunc(sysdate, 'yyyy')
and 1 = 1
The above was just a silly example; all I meant to say was that *somethimes* "1 = 1" (or 1 = 2) might help. I'm not saying that it can't be done differently, but - "1 = 2" obviously shows that thank you, I don't want this to return anything. 
|
|
|
|
Re: an SQL query [message #442434 is a reply to message #442386] |
Mon, 08 February 2010 00:56   |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
You cannot tell from the info provided that this is bad programming. I can imagine situations where one has little to no control over which queries are executed (eg Reports), but in order to force a query not to return anything, the lexical Littlefoot was mentioning is set to 1 = 2.
|
|
|
|
Re: an SQL query [message #442659 is a reply to message #442275] |
Tue, 09 February 2010 15:22   |
navin_deep
Messages: 18 Registered: November 2009
|
Junior Member |
|
|
i could not understand completely the concepts which Littlefoot started. Can anyone explain me OR give links on this topic to read myself ?
Thanks in Advance!!
regards
-navin
|
|
|
Re: an SQL query [message #442663 is a reply to message #442275] |
Tue, 09 February 2010 16:15  |
 |
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
Expressions like where 1 = 2 are used to stop a query from returning data.
There are several techniques for this but this technique in particular is liked because it short circuits the oracle optimizer. Oracle optimizer knows that 1=2 will never be true and thus never actually executes the query. This is the most efficient way to cause a query to not return data.
One reason why this is useful is in testing situations where a developer wants to check code to see if the syntax works.
Other than that I can think of no other value but that does not mean others smater than I can't give you one.
Kevin
|
|
|