I use C#, SQL Server and PetaPoco which auto increments the ID by default. I just want to know that if it is possible to set this autoincremented id to another column.
I have 2 more columns needs to be set with the exact value of ID. Is it possible to do this inside the SQL CREATE TABLE statement or doing some hook before or after ExecuteNonQuery? I don't want to create a view or a trigger if it's possible.
Yes, of course - the
CREATE TABLEallows you to define which column will be the auto-increment column, by adding theIDENTITYkeyword to the column definition. This works for any integer-based column, or columns of typeDECIMAL(p, 0)(with a0scale - no after-the-dot digits).E.g.
See the MS docs on
CREATE TABLEfor the full and complete detailsUPDATE: so you have 3 separate third-party applications, that all access this table, and all expect to find specific columns - one of which you can make the identity column. The two others could be computed, persisted "copies" of that identity column - something like this: