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 2008 Forums
 Transact-SQL (2008)
 Identity from other column

Author  Topic 

Rawler
Starting Member

9 Posts

Posted - 2011-01-28 : 16:34:49
Hi all!

I got a [city] table with these columns [idDepartment] [idCity] [nCity]; is there a way to set [idCity] an identity column, depending on which [idDepartment] was the insert?

ie:
[idDepartment] [idCity] [nCity] (before insert)
1, 1, 'myCity'
1, 2, 'aCity'
2, 1, 'myCity2'

and if I insert [idDepartment]=1, [nCity]='anotherCity', the result be:

[idDepartment] [idCity] [nCity] (result)
1, 1, 'myCity'
1, 2, 'aCity'
1, 3, 'anotherCity'
2, 1, 'myCity2'

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-02-11 : 10:15:26
You dont need that column. Just use it in SELECT Statement.

Refer method 2
http://beyondrelational.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 -