Increment primary key that is not an identity
Posted: Sat 20 Sep 2008 21:43
I have a mssql 2005 database table that someone else has created. The primary key has the following properties which I cannot change:
Name Unique ID
data type int
primary key true
allow nulls false
identity false
identity seed 0
identity increment 0
length 4
I need to insert a new row into the table making sure to use a primary key that is one more than the last one.
I have read about using this for the value of Unique ID in the insert statement.
isnull( (selectmax(Unique ID) from table), 0) +1 )
Then I must be able to retrieve the Unique ID in my application to work on the data row further. I am using C#, but any .net example should put me in the right direction.
I appreciate your advice.
Name Unique ID
data type int
primary key true
allow nulls false
identity false
identity seed 0
identity increment 0
length 4
I need to insert a new row into the table making sure to use a primary key that is one more than the last one.
I have read about using this for the value of Unique ID in the insert statement.
isnull( (selectmax(Unique ID) from table), 0) +1 )
Then I must be able to retrieve the Unique ID in my application to work on the data row further. I am using C#, but any .net example should put me in the right direction.
I appreciate your advice.