I have a requirement to automatically generate a column's value based on another query's result. Because this column value must be unique, I need to take into consideration concurrent requests. This query needs to generate a unique value for a support ticket generator.
The template for the unique value is CustomerName-Month-Year-SupportTicketForThisMonthCount
.
So the script should automatically generate:
- AcmeCo-10-2019-1
- AcmeCo-10-2019-2
- AcmeCo-10-2019-3
and so on as support tickets are created. How can ensure that AcmeCo-10-2019-1
is not generated twice if two support tickets are created at the same time for AcmeCo?
insert into SupportTickets (name)
select concat_ws('-', @CustomerName, @Month, @Year, COUNT())
from SupportTickets
where customerName = @CustomerName
and CreatedDate between @MonthStart and @MonthEnd;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…