【问题标题】:IF EXISTS in Setting a variable in SQLIF EXISTS 在 SQL 中设置变量
【发布时间】:2017-09-13 03:20:18
【问题描述】:

如何在设置变量时使用if exists 子句?

我已经设置好了,所以顶部选择语句的结果(@F1Runs, @F2Runs, @F3Runs,全部用于对应的Date)插入到临时表#WeekEnding,然后从那里插入到实际表中。

问题在于,对于某些日期,部分或全部熔炉没有“运行次数”结果。因此,当我设置变量@F1Runs、@F2Runs 和@F3Runs 时,我试图找到一种方法来放置一个 IF EXISTS 语句或类似的东西,以便当它们不存在时它们作为 0 插入到表中。

我得到的错误是:

无法将值 NULL 插入列“F2Runs”、表“WWALMDB.dbo.WeeklyRuns”;列不允许空值。插入失败。

我的代码:

Select 
    jr.FurnaceID, Count(Distinct jr.JobID) As 'Number of Runs'
Into 
    #WeekEnding
From 
    dbo.JobReports jr
Where 
    jr.StartDateTime >= @StartDate 
    and jr.EndDateTime < @Enddate
Group By  
    jr.FurnaceID
Order By 
    Count(jr.JobID) DESC

Select @F1Runs = [Number of Runs]
From #WeekEnding
Where FurnaceID = 1

Select @F2Runs = [Number of Runs] 
From #WeekEnding
Where FurnaceID = 2

Select @F3Runs = [Number of Runs] 
From #WeekEnding
Where FurnaceID = 3

If Exists (Select wr.WeekEnding
           From WWALMDB.dbo.WeeklyRuns wr
           Where wr.WeekEnding = DATEADD(day, -1, @Enddate))
Begin
    Update WWALMDB.dbo.WeeklyRuns 
    Set F1Runs = @F1Runs,
        F2Runs = @F2Runs, 
        F3Runs = @F3Runs
    Where WeeklyRuns.WeekEnding = DATEADD(day, -1, @Enddate)
End
Else
Begin
    Insert Into WWALMDB.dbo.WeeklyRuns (WeekEnding, F1Runs, F2Runs, F3Runs)
    Values (DATEADD(day, -1, @Enddate), @F1Runs, @F2Runs, @F3Runs)
End

【问题讨论】:

    标签: sql sql-server variables if-statement null


    【解决方案1】:

    我的理解是 IF Exists 需要一个块,所以我不相信这会对你有所帮助。

    我认为 ISNULL 是你的朋友

    Select jr.FurnaceID, Count(Distinct jr.JobID) As 'Number of Runs'
    Into #WeekEnding
    From dbo.JobReports jr
    Where jr.StartDateTime >= @StartDate and jr.EndDateTime < @Enddate
    Group By jr.FurnaceID
    Order By Count(jr.JobID) DESC
    
    SET @F1Runs = ISNULL(Select [Number of Runs]
    From #WeekEnding
    Where FurnaceID = 1,0)
    
    SET @F2Runs = (Select [Number of Runs] 
    From #WeekEnding
    Where FurnaceID = 2,0)
    
    SET @F3Runs  = (Select [Number of Runs] 
    From #WeekEnding
    Where FurnaceID = 3,0)
    
    If Exists ( Select wr.WeekEnding
                From WWALMDB.dbo.WeeklyRuns wr
                Where wr.WeekEnding = DATEADD(day, -1, @Enddate))
    Begin
        Update WWALMDB.dbo.WeeklyRuns 
        Set F1Runs = @F1Runs,
            F2Runs = @F2Runs, 
            F3Runs = @F3Runs
        Where WeeklyRuns.WeekEnding = DATEADD(day, -1, @Enddate)
        End
        Else
    Begin
        Insert Into WWALMDB.dbo.WeeklyRuns (WeekEnding, F1Runs, F2Runs, F3Runs)
        Values (DATEADD(day, -1, @Enddate), @F1Runs, @F2Runs, @F3Runs)
    End
    

    Select jr.FurnaceID, Count(Distinct jr.JobID) As 'Number of Runs'
    Into #WeekEnding
    From dbo.JobReports jr
    Where jr.StartDateTime >= @StartDate and jr.EndDateTime < @Enddate
    Group By jr.FurnaceID
    Order By Count(jr.JobID) DESC
    
    Select @F1Runs = [Number of Runs]
    From #WeekEnding
    Where FurnaceID = 1
    
    Select @F2Runs =[Number of Runs] 
    From #WeekEnding
    Where FurnaceID = 2
    
    Select @F3Runs = [Number of Runs] 
    From #WeekEnding
    Where FurnaceID = 3
    
    If Exists ( Select wr.WeekEnding
                From WWALMDB.dbo.WeeklyRuns wr
                Where wr.WeekEnding = DATEADD(day, -1, @Enddate))
    Begin
        Update WWALMDB.dbo.WeeklyRuns 
        Set F1Runs = ISNULL(@F1Runs,0),
            F2Runs = ISNULL(@F2Runs,0), 
            F3Runs = ISNULL(@F3Runs,0)
        Where WeeklyRuns.WeekEnding = DATEADD(day, -1, @Enddate)
        End
        Else
    Begin
        Insert Into WWALMDB.dbo.WeeklyRuns (WeekEnding, F1Runs, F2Runs, F3Runs)
        Values (DATEADD(day, -1, @Enddate), @F1Runs, @F2Runs, @F3Runs)
    End
    

    【讨论】:

    • 不知道为什么,但第一个选项带来了一些错误。不过,第二个有效。谢谢!
    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 2011-12-24
    • 2013-05-09
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多