Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do SELECT TOP @Param in a Stored Procedure?

People also ask

How do you select top and bottom rows in SQL?

You need to wrap the SELECT and FROM in a derived table and pull the WHERE to the outer query for this to work, like @Paul did in the second method in his answer.

How do I select top 10 rows in MySQL?

To select first 10 elements from a database using SQL ORDER BY clause with LIMIT 10. Insert some records in the table using insert command. Display all records from the table using select statement. Here is the alternate query to select first 10 elements.

How do I pass multiple parameters to a stored procedure in SQL Server?

Setting up multiple parameters is very easy to do. You just need to list each parameter and the data type separated by a comma as shown below.


Add parenthesis:

SELECT TOP (@NumberOfResultsToReturn) *

SQL Server: Put the argument in parens:

SELECT TOP (@NumberOfResultsToReturn) *

Here's how I was doing it in the old times:

SET @@ROWCOUNT = @NumberOfResultsToReturn
SELECT ........
SET @@ROWCOUNT = 0

This will work, although SELECT TOP (@NumberOfResultsToReturn) is preferable if you're using SQL server that supports this syntax:


This is supported in SQL Server 2005 and later, but not in SQL Server 2000. What version are you using?