简体   繁体   English

SQL Server中的自动增量列值

[英]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 我有两列SALARY_CODEFN_YEAR我想根据FN_YEAR列生成如下表:如果FN_YEAR值为18-19SALARY_CODE将为SAL/01-18-19 01-18-19,而下一个SALARY_CODE的值为18-19将为SAL/02-18-19

在此处输入图片说明

Please help me to write the SQL to perform the discussed task. 请帮助我编写SQL来执行所讨论的任务。

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 您可以使用row_number()并与另一列concatenate以获得所需的结果

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. 创建一个包含12行(假设需要所有月份)和一个索引列(其编号介于1和12之间)的表(我们将其命名为mnths),并创建另一个表用于所需年份。 then add rows from that table for each month and year, like this: 然后为每个月和每年在该表中添加行,如下所示:

Table name: mnths 表名称:mnths

Field name: mnt (values: 1-12) 字段名称:mnt(值:1-12)

Table name: years 表名:年

Field name: yr (values: 18-30) 字段名称:yr(值: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. Divya Agrawal的回答很好。 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. ID是您的表主键。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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