1

以下のコードで構文エラーが発生しています。エラーが発生している箇所はすべて太字にしています。if-else ステートメントを使用できますが、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 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 ステートメントを使用できますが、case ステートメントを使用したいです。

これを見るたびに、それが本当に問われている質問なのかどうか、よく考えます。なんで?

1 つのステートメントとして結合する必要がある場合は、条件を SELECT ステートメントに入れ、UNION ALL を使用してそれらを結合できます。そのうちの 1 つだけが実際に結果を生成します。

--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 の使用を主張する場合は、これが 1 つのオプションになる可能性があります。

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 に答える