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 2000 Forums
 Transact-SQL (2000)
 unique numbers

Author  Topic 

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2005-03-28 : 08:23:40
Hi,

I have a table in sql with the following fields:
Pid =identity primary key
Sid =int
RowNumber =int

I need that for each sid the rownumber will be unique but that the rownumber can repeat itself for a different sid. Also, I want to automatically enter the rownumber (the first entry must start at the value 1 ,second entry will be 2 etc.)
I have a asp.net application that inserts the values into the database but I don't want the users to enter the rownumber-I want it done automatically.

I need the table to look similar to this:
sid | RowNumber
12 | 1
12 | 2
12 | 3
14 | 1
14 | 2
etc

How can I do this.

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-28 : 08:56:11
Why do you feel you need to store this RowNumber value in your database? What are you using it for?

- Jeff
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-03-28 : 09:31:03
INSERT INTO <Your Table> (Sid, RowNumber)
SELECT @Sid, COALESCE((SELECT MAX(RowNumber) + 1 FROM <Your Table> WHERE Sid = @Sid), 1)

Dustin Michaels
Go to Top of Page

collie
Constraint Violating Yak Guru

400 Posts

Posted - 2005-03-30 : 02:46:58
Thanks that is exactly what I was looking for.
Go to Top of Page
   

- Advertisement -