CREATE TABLE [dbo].[Orders]( [ID] [int] IDENTITY(1,1) NOT NULL, [OrderDate] [date] NOT NULL, [CustomID] [int] NOT NULL, [Title] [nchar](50) NOT NULL ) ON [PRIMARY]
BEGIN TRAN declare @currentMaxID int declare @newID int select @currentMaxID = max(CustomID) from [dbo].[Orders] WITH (UPDLOCK) where year(OrderDate) = year(getdate()) and month(OrderDate) = month(getdate()) if @currentMaxID is null begin set @currentMaxID = 0 end set @newID = @currentMaxID + 1 insert [dbo].[Orders] (OrderDate, CustomID, Title) values (getdate(), @newID, 'my title') COMMIT