I can't seem to wrap my mind around a little situation I have. I am trying to set a variable using an if condition; I have also tried using a case statement as well, but keep receiving error. Below is what I am working with and it's dynamic SQL as well... The variable I am trying to set is @EBP_Allow... Can someone shine some light on this?
What I need it to do for example...
SELECT
@EBP_Allow = IF @Year > 2014 THEN DO a SELECT ELSE DO Something else...
What I have now...
SELECT
@OU_Allow = Optional_Unit_Allowed_Flag,
@BU_Allow = Basic_Unit_Allowed_Flag,
@EU_Allow = Enterprise_Unit_Allowed_Flag,
@WU_Allow = Whole_Farm_Unit_Allowed_Flag,
@EBP_Allow = I NEED TO USE SUB SELECT, IF OR CASE TO SET THIS
FROM dbo.@YEAR_Insurance_Offer
WHERE
(dbo.@YEAR_Insurance_Offer.State_Code = @StateCode) AND
(dbo.@YEAR_Insurance_Offer.County_Code = @CountyCode) AND
(dbo.@YEAR_Insurance_Offer.Crop_Code = ''@CropCode'') AND
(dbo.@YEAR_Insurance_Offer.Insurance_Plan_ID = @PlanId) @TypeCondition @PracticeCondition
Here's an update; it seems that when ran it's still jumping over my condition...
CASE
WHEN @YEAR < 2015
THEN ''N''
WHEN @YEAR > 2014
THEN (
SELECT Enterprise_Unit_By_Practice_Allowed_Flag
FROM dbo.@YEAR_Insurance_Offer
WHERE
(dbo.@YEAR_Insurance_Offer.State_Code = @StateCode) AND
(dbo.@YEAR_Insurance_Offer.County_Code = @CountyCode) AND
(dbo.@YEAR_Insurance_Offer.Crop_Code = ''@CropCode'') AND
(dbo.@YEAR_Insurance_Offer.Insurance_Plan_ID = @PlanId) @TypeCondition @PracticeCondition
)
END
If I replace the WHEN @Year > 2014
with this...
WHEN @YEAR > 2014
THEN ''N''
It work's just fine... For some reason or another when I have the select in there it's telling me that Enterprise_Unit_By_Practice_Allowed_Flag
is an invalid column, but it's not?