2016-09-10 119 views
1

我需要幫助,如果存在,如果不存在的條件。SQL Server如果條件基於日期

我想根據日期編寫一個if條件,這樣,如果某個作業的當前日期TARGET_DT的table1中存在任何行,那麼請執行一些操作 其他操作。

IF EXISTS(SELECT 1 FROM COD_BLU_OPENING_VOLUME 
      WHERE TARGET_DT = CONVERT(DATETIME, GETDATE(), 101) AND JOBID = @OPENJOB)   
BEGIN 
    IF(SELECT COUNT(1) FROM COD_BLU_INVENTORY WITH(NOLOCK) 
     WHERE (JOBID = @JOB AND STATUS = 'A' AND TARGETDT = CONVERT(DATETIME, @TARGETDT, 101))) = 0 
    BEGIN 
     INSERT INTO COD_BLU_INVENTORY(JOBID, TARGETDT, CARRYOVER, FRESHVOL, TOTALVOL, STATUS, UPDATEDBY, UPDATEDT, OPENINGVOL) 
     VALUES(@JOB, @TARGETDT, @CARRYOVERVOL, @FRESHVOL, @TOTALVOL, 'A', @EMPCODE, GETDATE(), @CARRYOVERVOL) 

     SELECT '1' 
    END 
    ELSE 
    BEGIN 
     SELECT'0' 
    END 
END    
ELSE IF NOT EXISTS(SELECT 1 FROM COD_BLU_OPENING_VOLUME 
        WHERE TARGET_DT = CONVERT(DATETIME, GETDATE(), 101) 
        AND TARGET_DT < CONVERT(DATETIME, GETDATE(), 101) 
        AND JOBID = @OPENJOB) 
BEGIN 
    INSERT INTO COD_BLU_OPENING_VOLUME(TARGET_DT, JOBID, OPENING_COUNT, UPDATED_BY, UPDATED_DT, PRE_ASSIGNEDCOUNT) 
    VALUES(@OPENTARDT, @OPENJOB, @OPENCNT, @OPENEMPCODE, GETDATE(), @PREOPENCNT) 

    IF (SELECT COUNT(1) FROM COD_BLU_INVENTORY WITH(NOLOCK) 
     WHERE (JOBID = @JOB AND STATUS = 'A' 
       AND TARGETDT = CONVERT(DATETIME, @TARGETDT, 101))) = 0 
    BEGIN 
     INSERT INTO COD_BLU_INVENTORY(JOBID, TARGETDT, CARRYOVER, FRESHVOL, TOTALVOL, STATUS, UPDATEDBY, UPDATEDT, OPENINGVOL) 
     VALUES(@JOB, @TARGETDT, @CARRYOVERVOL, @FRESHVOL, @TOTALVOL, 'A', @EMPCODE, GETDATE(), @CARRYOVERVOL) 

     SELECT '1' 
    END 
    ELSE 
    BEGIN 
     SELECT '0' 
    END 
END 

但問題是,如果TARGET_DT給定爲未來的日期,它執行else條件,我希望它執行if條件,上面是代碼,我累了。任何幫助解決這個將不勝感激。請幫忙。

+0

當'TARGET_DT'大於'CURRENTDATE'(未來日期)時,你想要執行IF部分。 –

+0

雅吉賓,另外我想檢查當前日期是否有行存在,並執行if條件 – rohini

+0

我發佈了一個答案,檢查它並讓我知道你是否有任何問題。 –

回答

0
IF EXISTS(SELECT 1 FROM COD_BLU_OPENING_VOLUME WHERE ((TARGET_DT=CONVERT(DATETIME,GETDATE(),101) OR TARGET_DT>CONVERT(DATETIME,GETDATE(),101)) AND [email protected]))   
     BEGIN 
      IF(SELECT COUNT(1) FROM COD_BLU_INVENTORY WITH(NOLOCK) WHERE ([email protected] AND STATUS='A' AND (TARGET_DT=CONVERT(DATETIME,GETDATE(),101) OR TARGET_DT>CONVERT(DATETIME,GETDATE(),101))))=0 
       BEGIN 
        INSERT INTO COD_BLU_INVENTORY(JOBID,TARGETDT,CARRYOVER,FRESHVOL,TOTALVOL,STATUS,UPDATEDBY,UPDATEDT,OPENINGVOL) 
        VALUES(@JOB,@TARGETDT,@CARRYOVERVOL,@FRESHVOL,@TOTALVOL,'A',@EMPCODE,GETDATE(),@CARRYOVERVOL) 
        SELECT '1' 
       END 
      ELSE 
       BEGIN 
        SELECT'0' 
       END 
      END    
    ELSE IF NOT EXISTS(SELECT 1 FROM COD_BLU_OPENING_VOLUME WHERE ((TARGET_DT=CONVERT(DATETIME,GETDATE(),101) OR TARGET_DT<CONVERT(DATETIME,GETDATE(),101)) AND [email protected])) 
     BEGIN 
      INSERT INTO COD_BLU_OPENING_VOLUME(TARGET_DT,JOBID,OPENING_COUNT,UPDATED_BY,UPDATED_DT,PRE_ASSIGNEDCOUNT) 
      VALUES(@OPENTARDT,@OPENJOB,@OPENCNT,@OPENEMPCODE,GETDATE(),@PREOPENCNT) 

      IF (SELECT COUNT(1) FROM COD_BLU_INVENTORY WITH(NOLOCK) WHERE ([email protected] AND STATUS='A' AND TARGETDT = CONVERT(DATETIME,@TARGETDT,101)))=0 
       BEGIN 
        INSERT INTO COD_BLU_INVENTORY(JOBID,TARGETDT,CARRYOVER,FRESHVOL,TOTALVOL,STATUS,UPDATEDBY,UPDATEDT,OPENINGVOL) 
        VALUES(@JOB,@TARGETDT,@CARRYOVERVOL,@FRESHVOL,@TOTALVOL,'A',@EMPCODE,GETDATE(),@CARRYOVERVOL) 
        SELECT '1' 
       END 
      ELSE 
       BEGIN 
        SELECT'0' 
       END 
     END 
    END 
+0

@rohini你能分享測試數據和你的預期結果嗎? –