Мой текущий синтаксис SQL похож на
Declare CursorName CURSOR FOR
Select Query
Теперь запрос выбора будет содержать условие If-Else.
If @Parameter1 is NULL
BEGIN
Select-Query1
END
ELSE
BEGIN
Select-Query2
END
Как написать вторую инструкцию If-Else внутрикурсор в SQL Server?
помогите пожалуйста!Дайте мне знать для моих входов. !!
Мой оригинальный запрос
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
)
НАЧАТЬ
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()) < 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()) > 30 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) <= 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()) > 60 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) <= 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()) > 90 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) <= 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()) > 120 AND DATEDIFF("dd", vwAgingPlan.SendDt,getDate()) <= 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()) > 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()) <= 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()) > 30 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) <= 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()) > 60 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) <= 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()) > 90 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) <= 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()) > 120 AND DATEDIFF("dd", vwAgingPlan.CreatedDt,getdate()) <= 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()) > 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
END
Мой запрос If-Else
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