title: If-Else Condition inside cursor
body: <p>My Current SQL Syntax is something like</p>
  
  <pre><code>Declare CursorName CURSOR FOR
   Select Query
  </code></pre>
  
  <p>Now the select query would contain an If-Else Condition.</p>
  
  <pre><code>If @Parameter1 is NULL
   BEGIN
    Select-Query1
   END
  ELSE
   BEGIN
    Select-Query2
   END
  </code></pre>
  
  <p>How to write second If-Else statement inside a cursor in SQL Server?</p>
  
  <p>please help!
  Let me know for my inputs.!!</p>
  
  <hr>
  
  <p><strong>My Original Query</strong>    </p>
  
  <pre><code>Create Table #TempTable(PlanID BIGINT,PlanName NVARCHAR(50),InsuranceCompany Nvarchar(100),CurrentBalance DECIMAL(14,2),
                          [30DaysBalance] DECIMAL(14,2),[60DaysBalance] DECIMAL(14,2),[90DaysBalance] Decimal(14,2),
                          [120DaysBalance] DECIMAL(14,2),[150DaysBalance] Decimal(14,2),CurrentDaysPlanAmount DECIMAL(14,2),
                          [30DaysPlanAmount] DECIMAL(14,2),[60DaysPlanAmount] DECIMAL(14,2),[90DaysPlanAmount] Decimal(14,2),
                          [120DaysPlanAmount] DECIMAL(14,2),[150DaysPlanAmount] Decimal(14,2),StartDate DateTime,EndDate DateTime
                         )
  </code></pre>
  
  <p>BEGIN</p>
  
  <pre><code>    Declare @BillID BIGINT,@PatientID BIGINT,@BillDetailID BIGINT,@SendDt DateTime
  
      Declare Cursor_Claim_PlanAgingReport Cursor 
  
      For Select Bill.BillID,Bill.PatientID,BillDetail.BillDetailID,Claim.SendDt From Bill Inner Join
          BillDetail On Bill.BillID = BillDetail.BillID Inner Join
          Claim on Bill.BillID = Claim.BillID Left Outer Join
          Payment On Bill.BillID = Payment.BillID 
          Where 
          ---Payment.BillID Is Null  AND
          Claim.SendDt 
          Between @StartDt AND @EndDt 
          ---And Claim.Status = 'Sent' 
          AND Claim.Status = 'Resent'
  
  
  
      Open Cursor_Claim_PlanAgingReport
  
      FETCH NEXT FROM Cursor_Claim_PlanAgingReport INTO @BillID,@PatientID,@BillDetailID,@SendDt
  
      While @@FETCH_STATUS = 0
      BEGIN
  
          Insert Into #TempTable SELECT Distinct(vwAgingPlan.PlanID),vwAgingPlan.Plan_Name,vwAgingPlan.Insurance_Company,         
  
          --// Current Balance --
          IsNull((SELECT top 1 vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &lt; 30 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS CurrentBalance,
  
          --// [30DaysBalance] --
          IsNull((SELECT top 1 vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &gt; 30 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &lt;= 60 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [30DaysBalance],
  
          --// [60DaysBalance] --
          IsNull((SELECT top 1 vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &gt; 60 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &lt;= 90 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [60DaysBalance],
  
          --// [90DaysBalance] --
          IsNull(
              (SELECT  top 1vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &gt; 90 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &lt;= 120 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [90DaysBalance],
  
          --// [120DaysBalance] --
          IsNull((SELECT top 1 vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &gt; 120 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &lt;= 150 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [120DaysBalance],
  
          --// [150DaysBalance] --
          IsNull((SELECT top 1 vwAgingPlan.Copay as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) &gt; 150 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [150DaysBalance],
  
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) &lt;= 30 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID),0) AS CurrentDaysPlanAmount,
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.CreatedDt, getdate()) &gt; 30 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) &lt;= 60 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID),0) AS [30DaysPlanAmount],
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.CreatedDt, getdate()) &gt; 60 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) &lt;= 90 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID),0) AS [60DaysPlanAmount],
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.CreatedDt, getdate()) &gt; 90 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) &lt;= 120 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID),0) AS [90DaysPlanAmount],
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd", vwAgingPlan.CreatedDt, getdate()) &gt; 120 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) &lt;= 150 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID),0) AS [120DaysPlanAmount],
          IsNull((SELECT top 1 vwAgingPlan.PlanAmount as s from vwAgingPlan WHERE DATEDIFF("dd",  vwAgingPlan.CreatedDt, getdate()) &gt; 150 And vwAgingPlan.BillID = @BillID And vwAgingPlan.PatientID = @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID), 0) AS [150DaysPlanAmount] ,
          @StartDt,@EndDt
      FROM 
          vwAgingPlan
      WHERE
          vwAgingPlan.BillID = @BillID AND vwAgingPlan.PatientID= @PatientID AND vwAgingPlan.BillDetailID = @BillDetailID
  
      FETCH NEXT FROM Cursor_Claim_PlanAgingReport INTO @BillID,@PatientID,@BillDetailID,@SendDt
  END
  
  
  Close Cursor_Claim_PlanAgingReport
  Deallocate Cursor_Claim_PlanAgingReport
  
  Select * From #TempTable    
  </code></pre>
  
  <p>END</p>
  
  <p><strong>My If-Else Query</strong></p>
  
  <pre><code>IF @InsuranceName IS NULL
  
  
  BEGIN
        SELECT Bill.BillID,
               Bill.PatientID,
               BillDetail.BillDetailID,
               Claim.SendDt,
               Claim.SendDT,
               InsurancePlan.Name
        FROM   Bill
               INNER JOIN BillDetail
                 ON Bill.BillID = BillDetail.BillID
               INNER JOIN Claim
                 ON Bill.BillID = Claim.BillID
               INNER JOIN Payment
                 ON Bill.BillID = Payment.BillID
               INNER JOIN dbo.InsurancePlan
                 ON dbo.BillDetail.PlanID = dbo.InsurancePlan.InsurancePlanID
               INNER JOIN dbo.InsuranceCompany
                 ON dbo.InsurancePlan.InsuranceCompID = dbo.InsuranceCompany.InsuranceCompID
        WHERE   
          Claim.SendDt BETWEEN @StartDt AND @EndDt
            AND Claim.Status = 'Resent'
               --OR Claim.Status = 'Resent'
  
        PRINT 'No Insurance Name'
    END
  ELSE
    BEGIN
        SELECT Bill.BillID,
               Bill.PatientID,
               BillDetail.BillDetailID,
               Claim.SendDt,
               Claim.SendDT,
               Claim.[Status],
               Payment.BillId AS PaymentBillID,
               InsurancePlan.Name
        FROM   Bill
               INNER JOIN BillDetail
                 ON Bill.BillID = BillDetail.BillID
               INNER JOIN Claim
                 ON Bill.BillID = Claim.BillID
               INNER JOIN Payment
                 ON Bill.BillID = Payment.BillID
               INNER JOIN dbo.InsurancePlan
                 ON dbo.BillDetail.PlanID = dbo.InsurancePlan.InsurancePlanID
               INNER JOIN dbo.InsuranceCompany
                 ON dbo.InsurancePlan.InsuranceCompID = dbo.InsuranceCompany.InsuranceCompID
        WHERE  InsurancePlan.Name = @InsuranceName
               --AND Payment.BillID IS NULL
               AND Claim.SendDt BETWEEN @StartDt AND @EndDt
               AND Claim.[Status]='Resent'  
  
        PRINT 'Insurance Name: ' + @InsuranceName
    END 
  </code></pre>