SQL Server如果条件基于日期

时间:2016-09-10 09:02:48

标签: sql-server if-statement

我需要帮助,给出if存在和不存在条件。

我想基于日期写一个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条件,上面是我厌倦的代码。任何帮助解决这个问题将不胜感激。请帮忙。

1 个答案:

答案 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 JOBID=@OPENJOB))          
        BEGIN
            IF(SELECT COUNT(1) FROM COD_BLU_INVENTORY WITH(NOLOCK) WHERE (JOBID=@JOB 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 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
    END