每天重置列值

时间:2016-09-17 03:47:00

标签: sql sql-server database

我的专栏需要从每天1000开始。对于每个新条目,此数字递增1.第二天,它应再次恢复为1000。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

CREATE TABLE [dbo].[GN_GnNm_Generate_Number_T](
    [GnNm_Code] [varchar](50) NOT NULL,
    [GnNm_Pad_With] [varchar](3) NOT NULL,
    [GnNm_No_Of_Times] [varchar](6) NOT NULL,    
    [GnNm_Increment_No] [int] NOT NULL,         /* Starting No eg:1 or 1000 */
    [GnNm_Recycle] [char](1) NOT NULL,         /* Recycle Type D-Daywise Y-yearwise M-Monthwise*/
    [GnNm_Recycle_Type] [varchar](1) NOT NULL,
    [GnNm_Recycle_Date] [datetime] NULL
) ON [PRIMARY]

GO

INSERT INTO GN_GnNm_Generate_Number_T
  VALUES ('V',  '0','5','1000','D','Y',GETDATE())

  /*  Run & Execute this store procedure for increment no. */
   /*  Automatically recycle everyday. */




 CREATE PROCEDURE [dbo].[usp_Genrate_Number]
    @Code_Text Varchar(50), @Gen_Pad varchar(3), @Recycle Varchar(1),  @Gen_Code Varchar(10)='' OUTPUT
AS
DECLARE @Pad_With Char(1), @Increment_No int, @No_Of_Times Varchar(6), @Gen_No Varchar(4),
        @Recycle_Type Varchar(1), @Recycle_Date Datetime, @EndofMonth Datetime, @EndofYear Datetime
BEGIN
    SELECT @Pad_With = GnNm_Pad_With , @No_Of_Times = GnNm_No_Of_Times,
           @Recycle_Type = GnNm_Recycle_Type, @Recycle_Date = GnNm_Recycle_Date
      FROM GN_GnNm_Generate_Number_T    
     WHERE GnNm_Code = @Code_Text

    SELECT @EndofMonth = DATEADD(MONTH,1+DATEDIFF(MONTH,0,@Recycle_Date),-1);
    SELECT @EndofYear = DATEADD(YEAR,1,@Recycle_Date)

    IF @Recycle = 'Y' 
    BEGIN
        IF @Recycle_Type = 'D' AND @Recycle_Date < CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME)
        BEGIN
            UPDATE GN_GnNm_Generate_Number_T
               SET GnNm_Increment_No = 1000,
                   GnNm_Recycle_Date = CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME)
             WHERE GnNm_Code = @Code_Text
        END 

        IF @Recycle_Type = 'M' AND CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME) > @EndofMonth
        BEGIN
            UPDATE GN_GnNm_Generate_Number_T
               SET GnNm_Increment_No = 0,
                   GnNm_Recycle_Date = CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME)
             WHERE GnNm_Code = @Code_Text
        END

        IF @Recycle_Type = 'Y' AND CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME) = @EndofYear         
        BEGIN
            UPDATE GN_GnNm_Generate_Number_T
               SET GnNm_Increment_No = 0,
                   GnNm_Recycle_Date = CAST(CONVERT(VARCHAR,GETDATE(),102)AS DATETIME)
             WHERE GnNm_Code = @Code_Text            
        END
   END      
       SELECT @Increment_No =  GnNm_Increment_No 
         FROM GN_GnNm_Generate_Number_T
         WHERE GnNm_Code = @Code_Text

       SET @Increment_No = @Increment_No + 1
       SELECT @Gen_Code = @Gen_Pad + REPLICATE(''+@Pad_With+'',@No_Of_Times - (LEN(@Increment_No)))+CONVERT(VARCHAR,@Increment_No)
            UPDATE GN_GnNm_Generate_Number_T
               SET GnNm_Increment_No = @Increment_No
             WHERE GnNm_Code = @Code_Text            

    SELECT @Gen_Code
    RETURN

END     

答案 1 :(得分:0)

假设表名为'Table_1'

自动增量列名称为“ID”

日期列名称为'EntryDate'(假设Table_1具有此列)

 DECLARE @NewId int = 0

 IF EXISTS (select * from Table_1 where EntryDate=GETDATE() and ID=1000)
   @NewId = (select max(ID) from Table_1) + 1
 ELSE
   @NewId = 1000

INSERT INTO TABLE_1(ID,EntryDate) VALUES (@NewId,GETDATE())