简体   繁体   中英

Auto Increment column values in SQL Server

I have two columns SALARY_CODE and FN_YEAR I want to generate table like below on the basis of FN_YEAR column: If FN_YEAR value is 18-19 then SALARY_CODE will be SAL/01-18-19 and next value of SALARY_CODE for 18-19 will be SAL/02-18-19

在此处输入图片说明

Please help me to write the SQL to perform the discussed task.

DECLARE @intCount int=0;
DECLARE @CustomID nvarchar(16);
DECLARE @fn_year nvarchar(16)='19-20';

SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year
IF(@intCount = 0)
BEGIN
    set @CustomID='SAL/1-'+ @fn_year
    insert into temptest values (@CustomID,@fn_year)
END
ELSE
BEGIN
        SET @CustomID= 'SAL/'+ CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year;
        insert into temptest values (@CustomID,@fn_year)
END

You can write function like below:

CREATE FUNCTION dbo.getSalaryCode(@fn_year NVARCHAR(16))  
RETURNS NVARCHAR(16)   
AS   
-- Returns the SALARY_CODE 
BEGIN  
    DECLARE @intCount int=0;
    DECLARE @CustomID nvarchar(16);

    SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year
    IF(@intCount = 0)
    BEGIN
        SET @CustomID='SAL/1-'+ @fn_year
    END
    ELSE
    BEGIN
        SET @CustomID= 'SAL/'+ CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year;
    END
    RETURN @CustomID;  
END; 

You can use row_number() and concatenate with the other column to get the desired result

with data as ( 
(select '18-19' FN_YEAR union all 
 select '18-19' union all 
 select '18-19' union all 
 select '19-20' union all 
 select '19-20' union all 
 select '20-21')   ) 
 select  FN_YEAR, 
        ('SAL/'+CAST((row_number() OVER (PARTITION BY FN_YEAR order by FN_YEAR)) AS VARCHAR(50))+'-'+FN_YEAR) 
        as SALARY_CODE from data

Check Demo Here

Create a table (let's call it mnths) with 12 rows (assuming all months are required) and an index colum with numbers between 1 and 12, and another table for the required years. then add rows from that table for each month and year, like this:

Table name: mnths

Field name: mnt (values: 1-12)

Table name: years

Field name: yr (values: 18-30)

SELECT 'SAL/'+CAST(mnt as nvarchar(2))+'-'+
CAST(yr as nvarchar(2))+'-'+CAST(yr+1 as nvarchar(2)) as SALARY_CODE, 
CAST(yr as nvarchar(2))+'-'+CAST(yr+1 as nvarchar(2)) as FN_YEAR
FROM mnths cross join years

If you already have the data, you can validate it against those tables and add missing entries.

Divya Agrawal answer is pretty good. But what i think you need is a trigger to fill those values automatically when you insert something on the table. The below procedure is a sample of how to pretty much do it. Id is your table primary key.

CREATE TRIGGER [dbo].[Salary_MyTable]
ON [dbo].[MyTable]
AFTER INSERT
AS
BEGIN
@Id
SELECT @Id = INSERTED.Id FROM INSERTED
SELECT @fn_year= INSERTED.FN_YEAR FROM INSERTED
SELECT @intCount = Count(SALARY_CODE) FROM temptest WHERE FN_YEAR=@fn_year

UPDATE [dbo].[MyTable] SET [SALARY_CODE]= 'SAL/' + CAST(@intCount+1 as nvarchar(16)) +'-'+@fn_year) WHERE @Id = Id

END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM