How can I add an identity number so that when a row is inserted an incremental number is assigned as below by a trigger? I am using SQL Server.
1 AAA
2 AAA
3 BBB
4 CCC
5 CCC
6 CCC
7 DDD
8 DDD
9 EEE
....
And I want to convert it to:
1 AAA 1
2 AAA 2
4 CCC 1
5 CCC 2
6 CCC 3
7 DDD 1
8 DDD 2
You could create a
FUNCTION
which get a name and givesMAX
identity for given parameter:and then set
DefaultValue
forNameId
for call the function when a record has been inserted like this:Assuming that
YourTable
is (Id
,Name
,NameId
).I hope to be helpful for you :)