c# - Multiple identity columns or a way to auto increment -
i have table (torder) has following structure in sql server 2008
orderid (int) - primary key , identity field. name(varchar) address(varchar) groupid (int) - field need auto increment, @ same time want able insert values into. my data like:
1 - john - address1 - 1 2 - mary - address2 - 1 3 - mary -address3 - 2 4 - jane - address4 - 3 where order ids 1 , 2 share same group , while 3 , 4 in own.
many orders can have same groupid, when insert order of new group, groupid auto populated next sequence number automatically, while @ same time allowing me insert duplicate groupid different orders if need to.
hope makes sense.
how go doing this? (i'm using c# in end, if makes difference)
one option create trigger on torder table (not fan of triggers, given criteria, can't think of option).
create trigger torder_trigger on torder after insert update torder set groupid = (select coalesce(max(groupid),0) + 1 torder) inserted i.groupid null , torder.orderid = i.orderid; this checks if inserted record has null groupid using inserted, , if so, updates table max(groupid) + 1, using coalesce check null.
Comments
Post a Comment