No. | Title |
---|---|
1 | Table With Running Number (SQL Server) |
2 | Table With Fixed Prefix + Running Number |
3 | Table Use Input As Prefix + Running Number |
4 | Table Use Input As Prefix + Counter Reset Counter When Different Prefix |
CREATE TABLE [dbo].[Customer](
[RunningNo] [bigint] IDENTITY(10000,1) NOT NULL,
[UserInput1] [varchar](50) NULL,
[UserInput2] [varchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY NONCLUSTERED
(
[RunningNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
b) [RunningNo] [bigint] IDENTITY(10000,1) NOT NULL,: This line defines a column named “RunningNo” of data type “bigint.” The “IDENTITY(10000,1)” indicates that the values in this column will be automatically generated in an incremental manner, starting from 10000 with a step of 1. The “NOT NULL” constraint ensures that this column cannot have a null value.
RunningNo | UserInput1 | UserInput2
--------------------------------------
10000 | Value1 | Value2
10001 | Value3 | Value4
CREATE TABLE [dbo].[Customer](
[RunningNo] [bigint] IDENTITY(10000,1) NOT NULL,
[ID] AS ('CUST-' + CAST([RunningNo] AS varchar(20))) PERSISTED,
[UserInput1] [varchar](50) NULL,
[UserInput2] [varchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY NONCLUSTERED
(
[RunningNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
RunningNo | ID | UserInput1 | UserInput2
------------------------------------------------------
10000 | CUST-10000 | Value1 | Value2
10001 | CUST-10001 | Value3 | Value4
CREATE TABLE [dbo].[Customer](
[RunningNo] [bigint] IDENTITY(10000,1) NOT NULL,
[ID] AS ([UserInput1] + '-' + CAST([RunningNo] AS varchar(20))) PERSISTED,
[UserInput1] [varchar](50) NULL,
[UserInput2] [varchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY NONCLUSTERED
(
[RunningNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]RunningNo ID UserInput1 UserInput2
10000 John-10000 John Doe
10001 Jane-10001 Jane Smith
RunningNo | ID | UserInput1 | UserInput2 ------------------------------------------------------ 10000 | John-10000 | John | Value1 10001 | Jane-10001 | Jane | Value2
CREATE TABLE [dbo].[Customer](
[RunningNo] [bigint] IDENTITY(10000,1) NOT NULL,
[Counter] [bigint] NOT NULL DEFAULT 10000,
[ID] [varchar](70) NULL,
[UserInput1] [varchar](50) NULL,
[UserInput2] [varchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY NONCLUSTERED
(
[RunningNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TRIGGER [dbo].[Trigger_Customer]
ON [dbo].[Customer]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @UserInput1 VARCHAR(50);
DECLARE @Counter BIGINT;
SELECT @UserInput1 = UserInput1 FROM inserted;
-- Check if the combination of UserInput1 already exists
IF EXISTS (
SELECT 1
FROM [dbo].[Customer]
WHERE UserInput1 = @UserInput1
)
BEGIN
-- Retrieve the current maximum Counter value for the combination
SELECT @Counter = MAX([Counter])
FROM [dbo].[Customer]
WHERE UserInput1 = @UserInput1;
END
ELSE
BEGIN
-- Combination does not exist, start Counter from the beginning
SET @Counter = 100000;
END
-- Increment the Counter for the combination
UPDATE [dbo].[Customer]
SET [Counter] = @Counter +1
WHERE UserInput1 = @UserInput1 AND [RunningNo] IN (SELECT [RunningNo] FROM inserted);
-- Update the inserted rows with the concatenated ID
UPDATE rh
SET ID = CONCAT(@UserInput1, '-', rh.[Counter])
FROM [dbo].[Customer] rh
INNER JOIN inserted i ON rh.RunningNo = i.RunningNo;
END
RunningNo | Counter | ID | UserInput1 | UserInput2
------------------------------------------------------
10000 | 10001 | CUST-10001 | CUST | Value1
10001 | 10002 | CUST-10002 | CUST | Value2
10002 | 10003 | CUST-10003 | CUST | Value1
10003 | 10001 | VEND-10001 | VEND | Value2
10004 | 10002 | VEND-10002 | VEND | Value2