Forums Register Login

Executing SQL query based on certain condition

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1638 times.
Similar Threads
Help on SQL Query
Help required for hibernate query (HQL) calling stored procedure
Help required for hibernate query (HQL) calling stored procedure
Projection from inner join
Hibernate: Different Join in Save() and Get()
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 23:54:13.