We are building an Issue program that allows users to enter Issues. They will use this program to enter trouble issues with our products (when customers call). One requirement is that upon opening a NEW issue, the Issue Number be displayed so the user can tell the customer their issue number without having to save it right away (for various reasons I won't get into). We don't need to reuse issue numbers but we have to "reserve" them when a user opens an issue (because if they save the issue, then that will be a the issueNumber the customer will use for future reference).
We created an Issue Table (IssueID being the IssueNumber and it's not auto-incrementing) and a IssueNumberQueue table. We thought we could use the IssueNumberQueue to track what IssueNumbers are being "reserved" and to know what is the next issue number.
So we need a way to generate the next available issue number for the program so that when the user selects "New Issue", they get the first available issue number that's not reserved or already used.
Here's what I started with but it could be all wrong:
Any ideas?
One thing is we do not have to use EVERY issue number....so let's say I have 5 users and they all open a new issue....issuenumbers 1, 2, 3, 4, & 5 would be "reserved".....if issue# 1, 3, & 5 get saved but issue#'s 2 & 4 don't, then we don't have to "re-use" #'s 2 &4, they would just be ignored and #6 would be the next available number.
We created an Issue Table (IssueID being the IssueNumber and it's not auto-incrementing) and a IssueNumberQueue table. We thought we could use the IssueNumberQueue to track what IssueNumbers are being "reserved" and to know what is the next issue number.
So we need a way to generate the next available issue number for the program so that when the user selects "New Issue", they get the first available issue number that's not reserved or already used.
Here's what I started with but it could be all wrong:
Code:
DECLARE @MaxIssueID int
SELECT @MaxIssueID = MAX(IssueID) + 1
FROM Issues
SELECT NextIssueNumber
FROM IssueNumberQueue
WHERE NextIssueNumber = @MaxIssueID
--IssueNumber not in use.
IF(@@ROWCOUNT = 0)
BEGIN
INSERT INTO IssueNumberQueue
VALUES (@MaxIssueID)
END
ELSE --IssueNumber in use, get another one.
BEGIN
--Some form of loop that will add 1 to the @MaxIssueID variable and search again to see if it's in use.
END
INSERT INTO IssueNumberQueue
VALUES (@MaxIssueID)
SELECT @MaxIssueID AS NextIssueNumber
Any ideas?
One thing is we do not have to use EVERY issue number....so let's say I have 5 users and they all open a new issue....issuenumbers 1, 2, 3, 4, & 5 would be "reserved".....if issue# 1, 3, & 5 get saved but issue#'s 2 & 4 don't, then we don't have to "re-use" #'s 2 &4, they would just be ignored and #6 would be the next available number.