Solved

SQL Query Request

Posted on 2010-08-27
6
179 Views
Last Modified: 2012-05-10
How do you populate an integer field with its row number.  I am interested in a query that populates a field with its row number.
0
Comment
Question by:dastaub
  • 2
  • 2
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33546468
Use ROW_NUMBER()

SELECT ID, ROW_NUMBER() OVER(ORDER BY ID) ROWNO
FROM YourTable

RowNumber will be created based on the order by column. Here I have given 'ID' as primary key field.

Raj
0
 

Author Comment

by:dastaub
ID: 33546491
i was expecting an update query?
0
 
LVL 41

Expert Comment

by:pcelba
ID: 33546517
You may populate the column immediately when you are adding it:

ALTER TABLE YourTable ADD YourNewColumn int identity(1,1)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33546520
See the attached scripts.
Raj
create table #table

(

	id int

)



insert into #table select 1 union select 4 union select 9



select * from #table



-- update query

update t

	set id = r.rowno

from #table t

	inner join 

(

	select id, row_number() over(order by id) rowno

	from #table

) r on t.id = r.id



select * from #table



drop table #table

Open in new window

0
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
ID: 33546546
If the column exists already then you may do it same way and then update the existing column and drop the identity column:

ALTER TABLE YourTable ADD YourNewColumn int identity(1,1)
GO
UPDATE YourTable SET YourOldIntColumn = YourNewColumn
GO
ALTER TABLE YourTable DROP COLUMN YourNewColumn
GO

The Update with ROW_NUMBER is also possible.
0
 

Author Closing Comment

by:dastaub
ID: 33546837
I was not aware that inserting the record number was involved, so i went with an identity column.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now