我在下面的代码中得到语法错误: 我在所有出现错误时都将其设置为粗体。我可以使用if-else语句,但我真的很想用case语句。 请帮我解决这个错误。SQL Server中的Case语句中的语法错误
**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 [email protected]_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 [email protected]_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 [email protected]_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**
SQL Server中的CASE/WHEN/THEN只能**用于**返回单个值** - 不要返回/执行整个代码块 –
尝试用'if @policy_type ='care'替换'case..when..then'语句.. else if @policy_type ='life'.. else if @policy_type ='home '..' –
您还需要使用'create table #PendingRequest ...'创建临时表,并用'insert into #PendingRequest select ...'填充表。在同一批次中不可能有多个“select ... into ..'。 –