Say I have a Customer table
id | customerId
1 569
2 569
1 577
1 555
2 555
3 555
On each insert I want id increment by one depend on the customerId column. I have prepared my insert sp as below, but I wonder if there is a native way to make my table behave like this (when creating table with any constraints).
ALTER dbo.ins_Customer
@CustomerId INT
AS
BEGIN
DECLARE @MaxSeqId INT
SELECT @MaxSeqId = MAX(id) FROM dbo.Customer
WHERE customerId = @CustomerId
INSERT INTO dbo.Customer VALUES (@MaxSeqId+1, @CustomerId)
END
I want this simple insert statement produce the result above with any possible, native constraints.
ALTER dbo.ins_Customer
@CustomerId INT
AS
BEGIN
INSERT INTO dbo.Customer VALUES (@CustomerId)
END