1

我在下面的代码中遇到语法错误:我在所有遇到错误的地方都加粗了。我可以使用 if-else 语句,但我真的想要用例语句。请帮助我解决错误。

**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**
4

3 回答 3

0

我可以使用 if-else 语句,但我真的想要用例语句。

每当我看到这一点时,我都会三思而后行,这是否是一个真正的问题。为什么?

如果必须将其组合为一个语句,则可以将条件放入 SELECT 语句中并使用 UNION ALL 将它们连接起来,其中只有一个会实际产生结果。

--declare @policy_type varchar(10), @employee_id int
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
AND @policy_type = 'car'
UNION ALL
Select policy_id,customer_id,policy_duration,requested_policy_amount,Age
          ,l.Illness
          ,l.Income
          ,premium_insurance_percentage,amount_insurance_percentage
          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
AND @policy_type = 'life'
UNION ALL
Select policy_id,customer_id,policy_duration,requested_policy_amount,home_Age
          ,home_Amount
          ,h.Area,home_premium_percentage 
          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
AND @policy_type = 'home';

如果这是一些愚蠢的分配要求,那么您可以对最后一行进行良性更改,例如

AND CASE @policy_type WHEN 'home' then 1 else 0 END = 1;
于 2012-11-14T07:32:39.713 回答
0

代替 case ,你可以使用 if 语句

if @policy_type = 'Car'
BEGIN
     Query1
END 
ELSE IF (@policy_type = 'life')
BEGIN
    Query2
END

因此你的#table 将在任何地方都可以访问

于 2012-11-14T07:52:40.907 回答
0

在这种情况下,使用 IF...ELSE 会容易得多。但是,如果您坚持使用 CASE,那么这可能是一种选择:

DECLARE @SQL varchar(max);
SELECT @SQL=
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

EXEC(@SQL);

拉吉

于 2012-11-14T06:42:56.377 回答