我正在尝试使用以下代码根据join
语句运行不同的CASE
查询。
因此,如果customer CLI
等于84422881
,我希望它根据[Extenstion]
字段加入,如果条件不满足,则ELSE
则加入在[Customer CLI]
use VoiceflexBilling
CASE WHEN [dbo].[MARU15_OWH07579_Calls].[CustomerCLI] = '84422881' THEN
UPDATE [dbo].[MARU15_OWH07579_Calls]
SET [dbo].[MARU15_OWH07579_Calls].[CustomerLookup] = CLIMapping.[customer id]
FROM [BillingReferenceData].[dbo].[CLIMapping]
INNER JOIN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls]
on [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[Extension] = [BillingReferenceData].[dbo].[CLIMapping].[CLI]
ELSE
UPDATE [dbo].[MARU15_OWH07579_Calls]
SET [dbo].[MARU15_OWH07579_Calls].[CustomerLookup] = CLIMapping.[customer id]
FROM [BillingReferenceData].[dbo].[CLIMapping]
INNER JOIN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls]
on [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[CustomerCLI] = [BillingReferenceData].[dbo].[CLIMapping].[CLI]
END
目前我收到以下错误:
消息156,级别15,状态1,行3关键字附近的语法不正确 ' CASE&#39 ;.消息156,级别15,状态1,行11附近的语法不正确 关键字' ELSE'。消息102,级别15,状态1,行19语法不正确 靠近' END'。
任何人都可以帮助使用正确的语法吗?
谢谢,
答案 0 :(得分:3)
对您的查询提出一些意见。首先,case
是表达式,而不是控制流。所以,它属于查询。其次,您可以使用“常规”逻辑进行此查询。第三,表别名会使查询更容易编写和阅读:
UPDATE c
SET c.[CustomerLookup] = CLIMapping.[customer id]
FROM [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls] cm
[BillingReferenceData].[dbo].[CLIMapping] c INNER JOIN
on (c.[CustomerCLI] = '84422881' AND c.[Extension] = cm.[CLI]) or
(c.[CustomerCLI] <> '84422881' AND c.[CustomerCLI] = cm.[CLI])
答案 1 :(得分:2)
您可以使用case
中的join condition
声明:
UPDATE [dbo].[MARU15_OWH07579_Calls]
SET [dbo].[MARU15_OWH07579_Calls].[CustomerLookup] = CLIMapping.[customer id]
FROM [BillingReferenceData].[dbo].[CLIMapping]
INNER JOIN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls]
ON [BillingReferenceData].[dbo].[CLIMapping].[CLI] =
CASE WHEN [dbo].[MARU15_OWH07579_Calls].[CustomerCLI] = '84422881'
THEN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[Extension]
ELSE [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[CustomerCLI]
END
答案 2 :(得分:0)
案例必须出现在查询中。对于您的场景,我建议您编写代码如下:(我需要实现循环迭代所有记录,在这种情况下,您需要稍微修改一下这段代码)
DECLARE @p varchar(100)
SET @p=(SELECT TOP 1 [dbo].[MARU15_OWH07579_Calls].[CustomerCLI] FROM [dbo].[MARU15_OWH07579_Calls])
IF @p = '84422881'
UPDATE [dbo].[MARU15_OWH07579_Calls]
SET [dbo].[MARU15_OWH07579_Calls].[CustomerLookup] = CLIMapping.[customer id]
FROM [BillingReferenceData].[dbo].[CLIMapping]
INNER JOIN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls]
on [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[Extension] =[BillingReferenceData].[dbo].[CLIMapping].[CLI]
ELSE
UPDATE [dbo].[MARU15_OWH07579_Calls]
SET [dbo].[MARU15_OWH07579_Calls].[CustomerLookup] = CLIMapping.[customer id]
FROM [BillingReferenceData].[dbo].[CLIMapping]
INNER JOIN [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls]
on [VoiceflexBilling].[dbo].[MARU15_OWH07579_Calls].[CustomerCLI] = [BillingReferenceData].[dbo].[CLIMapping].[CLI]