I am getting syntax error in the below code: I have made it bold where all I am getting an error. I can use the if-else statement but I really wanto use case statement. Pls help me with the error.
**CASE** @policy_type
WHEN 'car' THEN
(Select policy_id,customer_id,policy_duration,requested_policy_amount,Car_Age
,Car_Amount
,Number_of_Accidents,estimated_car_premium INTO
#PendingRequest FROM [dbo].[Policy] p join [dbo].[Car_Insurance] c on
p.policy_type_id=c.policy_type_id and p.approved_policy_amount is Null
--And employee_id=@employee_id
join
[dbo].[Car_Insurance_Estimate] c_est on car_age >= c_est.min_car_age and car_age <= c_est.max_car_age
and Car_Amount>=c_est.min_car_amount and Car_Amount<=c_est.max_car_amount and Number_of_Accidents>=c_est.min_accidents
and Number_of_Accidents<=c_est.max_accidents)
**WHEN** 'life' THEN
(Select policy_id,customer_id,policy_duration,requested_policy_amount,Age
,l.Illness
,l.Income
,premium_insurance_percentage,amount_insurance_percentage
INTO
#PendingRequest from [dbo].[Policy] p join [dbo].[life_Insurance] l on
p.policy_type_id=l.policy_type_id and p.approved_policy_amount is Null
And employee_id=@employee_id
join
[dbo].[life_Insurance_Estimate] l_est on age >= l_est.min_age and age <= l_est.max_age
and income>=l_est.min_income and income<=l_est.max_income and l.illness=l_est.illness)
when 'home' then
(Select policy_id,customer_id,policy_duration,requested_policy_amount,home_Age
,home_Amount
,h.Area,home_premium_percentage INTO
#PendingRequest
from [dbo].[Policy] p join [dbo].[home_Insurance] h on
p.policy_type_id=h.policy_type_id and p.approved_policy_amount is Null
And employee_id=@employee_id
join
[dbo].[home_Insurance_Estimate] h_est on home_age >= h_est.min_home_age and home_age <= h_est.max_home_age
and home_Amount>=h_est.min_home_amount and home_Amount<=h_est.max_home_amount and h.Area=h_est.area)
**END**
CASE/WHEN/THEN
in SQL Server can only be used to return a single value - not to return / execute entire code blocks – marc_s Nov 14 '12 at 6:17case..when..then
statements withif @policy_type = 'care'..else if @policy_type = 'life'..else if @policy_type = 'home'..
– James L. Nov 14 '12 at 6:32create table #PendingRequest ...
and fill the table withinsert into #PendingRequest select ...
. It is not possible to have multipleselect ... into ..
in the same batch. – Mikael Eriksson Nov 14 '12 at 6:42IF
is a statement.CASE
is an expression - it computes a value. – Damien_The_Unbeliever Nov 14 '12 at 7:40