2012-11-14 98 views
1

我在下面的代碼中得到語法錯誤: 我在所有出現錯誤時都將其設置爲粗體。我可以使用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** 
+6

SQL Server中的CASE/WHEN/THEN只能**用於**返回單個值** - 不要返回/執行整個代碼塊 –

+1

嘗試用'if @policy_type ='care'替換'case..when..then'語句.. else if @policy_type ='life'.. else if @policy_type ='home '..' –

+1

您還需要使用'create table #PendingRequest ...'創建臨時表,並用'insert into #PendingRequest select ...'填充表。在同一批次中不可能有多個「select ... into ..'。 –

回答

0

在這種情況下使用IF ... ELSE會容易得多。但是,如果你堅持使用的話,那麼這可能是一個選項:

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 [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 

EXEC(@SQL); 

拉吉

+0

使用此解決方案,臨時表只能在動態執行的範圍內訪問。任何在創建它之後應該完成的事情都必須是動態SQL的一部分。 –

+0

這只是給出一個替代解決方案的想法,以解決無法使用CASE根據參數值執行代碼塊的問題。當然,可能需要事先申報臨時表並填寫。 – Raj

0

我可以使用if-else語句,但我真的wanto使用case語句。

每當我看到這一點時,我會反覆思考這是否是一個真正的問題。爲什麼?

如果您必須將它合併爲一個語句,則可以將條件放入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 [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 
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 [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 
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 [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 
AND @policy_type = 'home'; 

如果是一些無聊的分配需求,那麼你可以做一個良性的變化到最後一排,說

AND CASE @policy_type WHEN 'home' then 1 else 0 END = 1; 
0

代替的情況下,u可以使用if語句

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

因此ur #table將在每個地方可訪問

+0

我很抱歉,但沒有解決方案正在工作。無論我使用什麼,它給我一個CASE或WHEN附近的語法錯誤。我以爲我可以輕鬆執行CASE聲明,但這需要很長時間。 – user1822832

+0

我的回答不包含女巫:) – AMH