Solved

Select by rownumber

Posted on 2013-05-10
8
287 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 125 total points
ID: 39155275
0
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 125 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 68

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
 
LVL 7

Assisted Solution

by:Ross Turner
Ross Turner earned 250 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 68

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 250 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 68

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now