Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 retrieve the absolute record from top

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-01-21 : 08:06:51
sreenivas writes "SQL SERVER VERSION:2000
I AM FACING ONE PROBLEM WHILE I AM DEVELOPING MY APPLICATION ON .NET..IT REQUIRES THAT TO RETRIEVE EXACT RECORD FROM TOP
IN THE TABLE i.e., 3rd record or 5th record BY USING DYNAMIC QUERY..TO ACCEPT NUMBER FROM THE USER ,I HOPE THERE IS NO REQUIREMENT FOR TABLE FILEDS..WHAT EVER THE FIELDS."

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2004-01-21 : 08:15:40
[code]
declare @top int

declare @sql varchar(1000)

set @sql = ''
set @top = 5 -- user input

set @sql = @sql + 'SELECT TOP ' + cast(@top as varchar(20)) + ' FROM myTable ORDER BY someField'

exec (@sql)
[/code]

________________
Make love not war!
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-01-21 : 08:25:16
with a * to get the record though.
And to get the single rec

declare @top int
declare @sql varchar(1000)

set @sql = @sql + 'select top 1 * from (SELECT TOP ' + cast(@top as varchar(20)) + ' * FROM myTable ORDER BY someField) a order by someField desc'

exec (@sql)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -