Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Running Auto Increment Column with Result

Author  Topic 

azamsharp
Posting Yak Master

201 Posts

Posted - 2010-01-04 : 10:36:14
How can I get a running column of incrementing integers along with my result?

Mohammad Azam
www.azamsharp.net

azamsharp
Posting Yak Master

201 Posts

Posted - 2010-01-04 : 10:42:38
got it I just used the row_number with over.

Mohammad Azam
www.azamsharp.net
Go to Top of Page

DP978
Constraint Violating Yak Guru

269 Posts

Posted - 2010-01-04 : 10:42:53
[code]DECLARE @myTable TABLE (myID int Identity, col1 CHAR(2), col2 CHAR(2))

Insert @myTable
Select 'A','A' Union all
Select 'A','B' Union all
Select 'B','A'

Select * from @myTable[/code]

Play around with "myID int Identity" to get desired results as well, the format is IDENTITY(Seed,Increment). So...
DECLARE @myTable TABLE (myID int Identity(2,3), col1 CHAR(2), col2 CHAR(2))

Starts at 2 and goes up by 3 each row.
Go to Top of Page

azamsharp
Posting Yak Master

201 Posts

Posted - 2010-01-04 : 11:16:59
@DP978,

In the above technique you will HAVE to insert into the @MyTable in order to generate the ID. The Row_Number with the over technique allows to generate this without requiring any TABLE.

Mohammad Azam
www.azamsharp.net
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-01-05 : 01:41:36
quote:
Originally posted by azamsharp

@DP978,

In the above technique you will HAVE to insert into the @MyTable in order to generate the ID. The Row_Number with the over technique allows to generate this without requiring any TABLE.

Mohammad Azam
www.azamsharp.net


Yes go for Row_number() function
Also see how it can be effectively used for other purposes as well
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -