?
Solved

Select by rownumber

Posted on 2013-05-10
8
Medium Priority
?
303 Views
Last Modified: 2013-05-10
Hello,
I would like to find a record by rownumber. How do I do that?

DECLARE @TempTable table(int id)
INSERT INTO @TempTable(id)value(11)
INSERT INTO @TempTable(id)value(151)
INSERT INTO @TempTable(id)value(17)

I would like to select row 2, i.e. get 151
0
Comment
Question by:johnson1
8 Comments
 
LVL 6

Assisted Solution

by:BurundiLapp
BurundiLapp earned 500 total points
ID: 39155275
0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 500 total points
ID: 39155289
This might work:

SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS RowNum, ID FROM @TempTable) sub
WHERE RowNum = 2
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155294
row_number() over ... applies to the retrieved rowset. You can't address the 2nd row ever inserted directly with it.
You will need a numeric column with the IDENTY property set. That column will auto-increment on each INSERT (only), and can then be selected for.
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 1000 total points
ID: 39155320
Here is an Example....

http://sqlfiddle.com/#!3/89cd0/10

Create Table TempTable 
(id int)

INSERT INTO TempTable(id)values(11);
INSERT INTO TempTable(id)values(151);
INSERT INTO TempTable(id)values(17);

Open in new window


Select 
* 
From 
(select 
ID,
ROW_NUMBER() over (order by (SELECT 1)) AS rownumber
from temptable
) as t
where rownumber = 2

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155339
The order of rows is not static, so that is not reliable. If there is an index, or even without, the order might change with later inserts.
0
 
LVL 7

Accepted Solution

by:
Ross Turner earned 1000 total points
ID: 39155369
Wouldn't it make more sense to add an identity field

http://sqlfiddle.com/#!3/6a8bb/1


Create Table TempTable 
(ident int identity,
  id int)

INSERT INTO TempTable(id)values(11);
INSERT INTO TempTable(id)values(151);
INSERT INTO TempTable(id)values(17);

Open in new window


Select * from Temptable
where ident = 2

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39155382
Ross,
see http:#a39155294 ;-).
0
 
LVL 7

Expert Comment

by:Ross Turner
ID: 39155387
lol yeah what you said :)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question