Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to limit the sql to reurn only 1 result

Status
Not open for further replies.

avihaimar

Programmer
Jul 30, 2003
38
IL
Hey,

My application give the user the ability to create a virtual view.

The user insert a query - "selcet product_id from product" and we show him the structre of this virtual view.

In order to get the column types i run this query than get the "ResultSetMetaData" ( by getMetaData() ) and finally get the column type from the ResultSetMetaData.

I dont need the full results for the query, so i want to limit the results for 1.
Does any one has an idea?

Thank you
 
Select Top 1 product_id from product

I tried to have patience but it took to long! :) -DW
 
Which database engine is your user using? Is it Access, Oracle, SQL Server etc? There are various solutions, but they are engine specific.

John
 
I need a generic solution.
currently i solved it by setting stmt.setMaxRows(1), but i read that it dosnt gurrenty better performance .
 
jadams0173's solution will work in Microsoft databases (Access or SQL server). Probably the nearest to a completely database engine independent solution is something like:

SELECT min (product_id) As [Product ID]
FROM product
WHERE condition

Important: You must set the where clause here to only retrieve data from the rows that you need, or you will get the smallest product ID in the table every time.

John
 
And if you have multiple records then what criteria determines which one you show? Ids it the newest one, the oldest one or the one with a specific value in one field? This can dictate waht you add to the query in where clause to get the proper results.

Questions about posting. See faq183-874
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top