This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Executing SQL query based on certain condition Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Executing SQL query based on certain condition" Watch "Executing SQL query based on certain condition" New topic
Author

Executing SQL query based on certain condition

Harish V Gupta
Ranch Hand

Joined: Dec 17, 2011
Posts: 31
I want to get data from a table based on certain condition. For eg, if table1 is not empty then get complete data from table2 else get the data from table2 for which created_dt (let's say it's one of the columns in table2) is today's date. It's like following :

if(condition)
then execute query1
else
execute query2

Is there any way to achieve this in SQL?
James Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 105
Hi Harish V Gupta,

You can use count(*) method to know the number of rows depending on that result you can write the query.

Regardss,
James


Whizlabs-Software, OCAJP 7 , SCJA 6 , SCJP 6 , OCPJP 7 , OCPJP 7- Upgrade ,SCWCD 5 ,OCEJWCD 6 ,SCBCD 5 ,SCEA 5 , PMP , PMI-ACP
Unconditional 100% Test PASS Guarantee!
Harish V Gupta
Ranch Hand

Joined: Dec 17, 2011
Posts: 31
Thanks for the reply James. You are right that count(*) will give me total number of records. But I am not able to figure out how to use that value in a condition for choosing between queries.
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 107

If I understand correctly, I think you are trying to evaluate condition at the database layer. Isn't the condition a part of business layer ? The job of the lowest layer of ORM must be to execute a query. Which query has to be executed must be decided at the business layer. In short, you can use count(*) to evaluate the ccondition and use the ORM to execute the query.


Thanks,
Tushar (SCJP 1.5)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing SQL query based on certain condition
 
Similar Threads
Projection from inner join
Help required for hibernate query (HQL) calling stored procedure
Help on SQL Query
Hibernate: Different Join in Save() and Get()
Help required for hibernate query (HQL) calling stored procedure