December 4, 2003 at 10:48 am
I was wondering if it was possible to have a variable when using TOP or if there is another way I can achieve my wanted result. Ex. (select TOP x from Employee) I only want to return the first few records of a randomly generated list, but the number of records that need to be selected will vary depending on a provided date. Thanks
December 4, 2003 at 11:01 am
Where's a way of doing it...
declare @iRows int,
@nvsql nvarchar(200)
set @iRows = 10
set @nvsql = 'select top ' + convert(char,@iRows) + ' * from TableName'
exec sp_executesql @nvsql
December 4, 2003 at 11:02 am
Or:
declare @iRows int
set @iRows = 10
EXEC ('SELECT TOP ' + convert(char,@iRows) + ' * from TableName')
December 4, 2003 at 12:33 pm
OR Use Jonathan's recomendation in this thread:
* Noel
December 4, 2003 at 3:37 pm
If you use SET ROWCOUNT @variable, make sure you SET ROWCOUNT 0 after your query.
Brian
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply