2017-09-13 1 views
0

Comment avoir une clause if exists dans la définition d'une variable?SI EXISTS dans Définition d'une variable dans SQL

Je possède ce mis en place afin que les résultats de l'instruction select top (@F1Runs, @F2Runs, @F3Runs, tous pour un correspondant Date) insérer dans la table temporaire #WeekEnding, puis de là dans une table réelle. Le problème est que, pour certaines dates, il n'y a pas de 'nombre de cycles' pour certains ou tous les appareils de chauffage. Donc, j'essaie de trouver un moyen de mettre une instruction IF EXISTS ou quelque chose comme ça quand je mets les variables @ F1Runs, @ F2Runs, et @ F3Runs, de sorte qu'ils insèrent dans la table 0 quand ils n'existent pas.

L'erreur que je reçois est:

Impossible d'insérer la valeur NULL dans la colonne 'F2Runs', table 'WWALMDB.dbo.WeeklyRuns'; La colonne n'autorise pas les valeurs NULL. INSERT échoue.

Mon code:

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 

Répondre

0

Je crois comprendre que si Exists va exiger un bloc, donc je ne t croient que cela vous aidera.

Je pense que ISNULL est votre ami ici

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 

OU

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 
+0

Je ne sais pas pourquoi, mais la première option a quelques erreurs. Le deuxième fonctionne, cependant. Je vous remercie! – MadelineK