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)
 Incremented column for a primary key

Author  Topic 

TheMick15
Starting Member

4 Posts

Posted - 2008-10-17 : 10:02:22
I have lots of IDs for a particular primary key column. Is there a way to create a column for each of these IDs that will increment an integer say 1-3 for each ID? So each of my primary keys will be repeated 3 times with the numbers 1-3 next to them.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-17 : 10:05:04
you cant repeat primary key values. they must be unique. whats the purpose behind trying to do this?
Go to Top of Page

TheMick15
Starting Member

4 Posts

Posted - 2008-10-17 : 10:09:27
Well it is a primary key in another table, so it can be repeated in this table, I am trying to fill those numbered columns with shipments based on their date, so that i can then use it to specify shipment 1, shipment 2, shipment 3 for a given ID
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-17 : 10:17:16
quote:
Originally posted by TheMick15

Well it is a primary key in another table, so it can be repeated in this table, I am trying to fill those numbered columns with shipments based on their date, so that i can then use it to specify shipment 1, shipment 2, shipment 3 for a given ID


do you really need to store this in table. if not, you can create this sequence in select queries using row_number() function in sql 2005.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-17 : 10:24:33
if you want to store, code will be like

UPDATE t
SET t.Sequence=t1.seq
FROM table t
(SELECT ROW_NUMBER() OVER (PARTITION BY ForeignKey ORDER BY PrimaryKey) AS Seq,*
FROM table)t1
on t.primarykey=t1.primarykey
Go to Top of Page
   

- Advertisement -