Sql server SQL Server中Case语句中的语法错误
以下代码中出现语法错误: 当我遇到一个错误的时候,我就大胆了。我可以使用if-else语句,但我确实想使用case语句。 请帮我纠正错误Sql server SQL Server中Case语句中的语法错误,sql-server,sql-server-2008,syntax-error,case,Sql Server,Sql Server 2008,Syntax Error,Case,以下代码中出现语法错误: 当我遇到一个错误的时候,我就大胆了。我可以使用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_Acci
**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**
在这种情况下,使用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);
拉吉
我可以使用if-else语句,但我确实想使用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 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';
可以使用if语句代替case
if @policy_type = 'Car'
BEGIN
Query1
END
ELSE IF (@policy_type = 'life')
BEGIN
Query2
END
因此,SQL Server中的每个位置都可以访问您的表,CASE/WHEN/THEN只能用于返回单个值,而不是返回/执行整个代码块,替换CASE..WHEN..THEN语句为if@policy_type='care'..else if@policy_type='life'..else如果@policy_type='home'..您还需要创建临时值正在使用create table PendingRequest创建表。。。并在表格中填入insert into PendingRequest select。。。。不可能有多个选择。。。进入在同一批中。IF是一个语句。CASE是一个表达式-它计算一个值。使用此解决方案,只能在动态执行的范围内访问临时表。在创建后,任何应该使用它的操作都必须是动态SQL的一部分。这只是为了提供一个替代解决方案的想法,以解决无法使用基于参数值的代码块执行用例的问题。当然,可能需要先声明临时表并进行填充。很抱歉,这些解决方案都不起作用。无论我使用什么,它都会在CASE或WHEN附近给我一个语法错误。我想我可以很容易地执行一个案例陈述,但这需要很长时间。我的答案不包含:
if @policy_type = 'Car'
BEGIN
Query1
END
ELSE IF (@policy_type = 'life')
BEGIN
Query2
END