2016-09-17 12 views
0

私のカラムは毎日1000から開始する必要があります。新しいエントリごとに、この番号は1ずつ増加します。翌日には、再び1000に戻ります。これをどうすればいいのですか?毎日カラム値をリセットする

答えて

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(''[email protected]_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  
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()) 
であるです
関連する問題