Solved

Find id number

Posted on 2011-09-29
8
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

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 60

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

737 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