Possible to implement a manual increment with just simple SQL INSERT?

后端 未结 11 1818
刺人心
刺人心 2021-01-05 11:54

I have a primary key that I don\'t want to auto increment (for various reasons) and so I\'m looking for a way to simply increment that field when I INSERT. By simply, I mean

11条回答
  •  北海茫月
    2021-01-05 12:19

    It could be because there are no records so the sub query is returning NULL...try

    INSERT INTO tblTest(RecordID, Text) 
    VALUES ((SELECT ISNULL(MAX(RecordID), 0) + 1 FROM tblTest), 'asdf')
    

提交回复
热议问题