sql server - How to use SEQUENCE to only insert unique numbers? -


i working pallets , range id of 10000000 15000000. cycles few times each year , current solution have table current pallet_id trigger resets @ 15000000 10000000 , not problem because time happened numbers free again. issue ,

i have been looking using sequence instead of using table trigger.

what have come far is:

sp_counter_nextvalue

create sequence [database].sp_counter_nextvalue int start 10000000 increment 1 minvalue 10000000 maxvalue 15000000 cycle 

insert

insert x_pallets (sp_counter_nextvalue, information) 

how go skipping number example when number 10000000 exists in x_pallets when counter reaches maximum value?

here's final solution.

sequence

create sequence [dbo].[seq_pallet]      [bigint]     start 10010341     increment 1     minvalue 10000000     maxvalue 15000000     cycle      cache  go  create sequence [dbo].[seq_pick_pallet]      [bigint]     start 5541980     increment 1     minvalue 1000000     maxvalue 9999999     cycle      cache  go 

stored procedure

alter procedure [dbo].[sp_counter_nextvalue]     @name nvarchar(24) begin     set nocount on;      declare @value bigint      if @name = 'pallet'     begin                select @value = next value seq_pallet     end     else if @name = 'pick_pallet'     begin         select @value = next value seq_pick_pallet     end      select @value value     return @value end 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -