What’s the best way to keep track of last generated column value in the following scenario:
I have a name
column in the BoxLocation
table of MS SQL server(DDL shown below)
On the user interface, user has an option to select A or B or C from the dropdown list and based on this selection, I want to populate an HTML input text field as follows:
Scenario 1:
For the very first time, where there is nothing in the database table and user selects A from the dropdown, I want to populate A-1 in the name input HTML text field using javascript.
Scenario 2:
After first record has been inserted, the name fied in the table will containe A-1. So I if user selects A from the dropdown again, I want to populate A-2 this time in the input HTML
field.
Similary, user can do after selecting B or C from the list so I will have to display B-1(if it’s first time) or B-2 (for second time) of if C is selected,
then C-1(if it’s first time) or C-2 (for second time)
Should I consider adding a field in the database table like lastUsed and get the next possible value based on the user’s selection? or what’s the best approach to achieve the same?
Here’s the DDL:
USE [boxdatabase]
GO
/****** Object: Table [dbo].[BoxLocation] Script Date: 7/13/2023 10:47:19 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[BoxLocation](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NOT NULL,
[description] [varchar](250) NOT NULL,
[comment] [varchar](250) NULL,
[locationId] [int] NULL,
[capacity] [int] NULL,
[isFull] [bit] NULL,
[entryDate] [datetime] NULL,
[endDate] [datetime] NULL,
CONSTRAINT [PK_BoxLocation] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BoxLocation] ADD CONSTRAINT [DF_BoxLocation_entryDate] DEFAULT (getdate()) FOR [entryDate]
GO