How to get the next number in a sequence

前端 未结 9 1062
死守一世寂寞
死守一世寂寞 2020-12-04 01:48

I have a table like this:

+----+-----------+------+-------+--+
| id | Part      | Seq  | Model |  |
+----+-----------+------+-------+--+
| 1  | Head      | 0         


        
9条回答
  •  一整个雨季
    2020-12-04 02:17

    Since you want the sequence to be based on the a specific model, just add that into the where clause when doing the select. This will ensure the Max(SEQ) pertains only to that model series. Also since the SEQ can be null wrap it in a ISNULL, so if it is null it will be 0, so 0 + 1, will set the next to 1. The basic way to do this is :

    Insert into yourtable(id, Part, Seq, Model)
        Select 6, 'Groin', ISNULL(max(Seq),0) + 1, 3 
        From yourtable
        where MODEL = 3;
    

提交回复
热议问题