IF EXISTS在SQL中设置变量

时间:2017-09-13 03:20:19

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

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

我有这个设置,所以顶部选择语句(@F1Runs, @F2Runs, @F3Runs的结果,所有对应的Date)的结果插入临时表#WeekEnding,然后从那里插入到实际表。

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

我得到的错误是:

  

无法将值NULL插入列' F2Runs',table' WWALMDB.dbo.WeeklyRuns&#39 ;;列不允许空值。 INSERT失败。

我的代码:

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

1 个答案:

答案 0 :(得分:0)

据我所知,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

OR

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