Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Hi,Can I take the [index] result from a table and store it in a local var?For example a tabledeclare @keyWords( word varchar(40), wordLen smallint)word len---- ---test 4t 1e 1declare @index smallInt = 2SET @keyWord = SELECT TOP @INDEX word FROM @KeyWordsThis obviously is not correct for several reasons (TOP returns multiple results, TOP doesn't work with local var etc)
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts
Posted - 2008-04-30 : 05:01:38
Yes. Add identity column to the table or use new ROW_NUMBER() function.
declare @t table(id int identity(1,1),word varchar(40),wordLen smallint)insert @t(word, wordlen)select 'test', 4 union allselect 't', 1 union allselect 'e', 1select * from @t where id = 2
Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED"
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-04-30 : 05:02:36
[code]SELECT t.word.t.len FROM(SELECT ROW_NUMBER() OVER(ORDER BY len DESC) AS RowNo,word,lenFROM @keyWords)tWhere t.RowNo=@index[/code]