2012-11-14 62 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