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條件,上面是代碼,我累了。任何幫助解決這個將不勝感激。請幫忙。
當'TARGET_DT'大於'CURRENTDATE'(未來日期)時,你想要執行IF部分。 –
雅吉賓,另外我想檢查當前日期是否有行存在,並執行if條件 – rohini
我發佈了一個答案,檢查它並讓我知道你是否有任何問題。 –