Solved

Find id number

Posted on 2011-09-29
8
280 Views
Last Modified: 2012-05-12
How can I get id number in any particular row sql? My table does no have ID field.  My data is already insert to a table.
0
Comment
Question by:VBdotnet2005
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 36817738
you want to add identity column? or just number the rows?
0
 

Author Comment

by:VBdotnet2005
ID: 36818307
I want to find out identiy number. Is it possible. My table right now does not have id column.
0
 
LVL 15

Expert Comment

by:Eyal
ID: 36818337
I don't understand what you need.

you want to add a new identity column to the table?
or only in the select display row numbers?

point me to the right direction you need
0
 

Author Comment

by:VBdotnet2005
ID: 36818665
I don't want add new columns. My table right now does not have id column. Maybe I am asking the wrong question. I want to find out the row # on a particular row.
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 32 total points
ID: 36819042
>>I want to find out the row # on a particular row. <<
There is no such thing as a row #.  You can certainly create one, but the order will depend on the ORDER BY clause.  For example you can do something like this:
SELECT ROW_NUMBER() OVER (ORDER BY SomeColumn) Row, *
FROM YourTableNameGoesHere
ORDER BY SomeColumn
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 62 total points
ID: 36839538
Exactly as acperkins has just shown. Here is an explanation of how that works: http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/A_1555-Analytical-SQL-Where-do-you-rank.html

Another approach is to ADD an IDENTITY to your table. It will assign sequential number in the natural order of the rows currently.

You can try this with:

SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) Row, *
FROM YourTableNameGoesHere
;

But not sure that the order can be depended on to always match up to the order rows were inserted originally.
0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 31 total points
ID: 36890249
If your Table doesn't have a ID-column
Dos it have a Primary Key (can be over multiple Columns?)

The Id field is mostly used to retreive very fast the Row that is needed using this Key
And to Link the Row to other rows that needs the data of our Row (See Joins)

If your table doen's have a I field you can always check if you have a Primary Key
Using this Primary Key you can do the same (even when containing multiple Columns)

poor beggar
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 62 total points
ID: 36890258
Re: "Another approach is to ADD an IDENTITY to your table. It will assign sequential number in the natural order of the rows currently." I just caught that you said "I don't want add new columns." Therefore, never mind that, but the addition to what acperkins still applies. That is likely your best approach. If you do have some column that is sequential/chronological, then you can use that in the OVER(ORDER BY) clause as acperkins showed; otherwise, try the (SELECT 1).
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

863 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

21 Experts available now in Live!

Get 1:1 Help Now