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

后端 未结 11 1828
刺人心
刺人心 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:29

    declare @nextId int
    set @nextId = (select MAX(id)+1 from Table1)
    
    insert into Table1(id, data_field) values (@nextId, '[blob of data]')
    
    commit;
    

    But perhaps a better approach would be using a scalar function getNextId('table1')

提交回复
热议问题