有些人可以帮助我,告诉我我做错了什么

时间:2014-06-20 15:27:32

标签: sql sql-server

CREATE TABLE DateRange
([Date] DATETIME, IsFutureOrPast BIT)

    DECLARE @Date AS DATETIME
    SET @DATE ='2014-01-01 00:00:00.000'
    WHILE @DATE <='2014-12-31 00:00:00.000'
        BEGIN
            INSERT INTO DateRange ([DATE])
            VALUES (@DATE)
            SET @Date = @Date + 1
        END

--Case Statement to check values and enter flag
    DECLARE @CurrentDate AS DATETIME = GETDATE()
        UPDATE 
            DateRange
        SET  [DATE] =
             CASE
                WHEN @CurrentDate <= [DATE] THEN 0
                ELSE 1
                END AS IsPastOrFuture
                FROM DateRange
                WHERE [Date] IN ( 0, 1)

3 个答案:

答案 0 :(得分:1)

您应该在设置DATE字段时尝试设置IsFutureOrPast字段:

    UPDATE 
        DateRange
    SET  [IsFutureOrPast] =
            CASE WHEN @CurrentDate <= [DATE] THEN 0
            ELSE 1
            END  

但你也可以将这两个陈述结合起来:

        INSERT INTO DateRange ([DATE], [IsFutureOrPast])
        VALUES (
            @DATE, 
            CASE WHEN GETDATE() <= @DATE THEN 0 ELSE 1 END  
        )
        SET @Date = @Date + 1

答案 1 :(得分:0)

试试这个:

DECLARE @DateRange TABLE
([Date] DATETIME, IsFutureOrPast BIT)

    DECLARE @Date AS DATETIME
    SET @DATE ='2014-01-01 00:00:00.000'
    WHILE @DATE <='2014-12-31 00:00:00.000'
        BEGIN
            INSERT INTO @DateRange ([DATE])
            VALUES (@DATE)
            SET @Date = @Date + 1
        END

--Case Statement to check values and enter flag
    DECLARE @CurrentDate AS DATETIME = GETDATE()
        UPDATE 
            @DateRange
        SET  IsFutureOrPast =
             CASE
                WHEN @CurrentDate <= [DATE] THEN 0
                ELSE 1
                END 
                FROM @DateRange


SELECT * FROM @DateRange

答案 2 :(得分:0)

我认为您正在尝试SET IsPastOrFuture

DECLARE @CurrentDate AS DATETIME = GETDATE()
    UPDATE 
        DateRange
    SET 
        IsPastOrFuture = CASE
                             WHEN @CurrentDate <= [DATE] THEN 0
                             ELSE 1
                         END