I have two columns in a table of a SQL server DB that I would like to autoincrement when new fields are added. However, Managment Studio wont allow me to set two columns to IDENTITY. Is there some other way to do this?
+4
A:
You could make the second field a calculated field based on the first.
Or make an INSERT trigger than programatically generates the value for the second.
BradC
2010-09-01 22:44:04
Yeah I think the trigger would probably be the best option. It doesn't need to follow any specific formula.
Eric Anastas
2010-09-01 22:56:14
+1
A:
If you wanted the 2nd column to basically be a mirror of the first:
ALTER TABLE dbo.myTable ADD
foo AS [rowid]
GO
If you wanted it to apply some math formula to it to achieve some kind of offset:
ALTER TABLE dbo.myTable ADD
foo AS ([rowid]+1) * 7 --or whatever you like.
GO
p.campbell
2010-09-01 22:52:48