2017-03-04 97 views
0

以下select語句正常工作。在UPDATE查詢中使用select sub

SELECT * 
FROM [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST] A 
WHERE EXISTS (SELECT 1 
       FROM [BillingReferenceData].[dbo].[CLIMapping] B 
       WHERE A.CustomerCLI = B.CLI AND A.CustomerCLI = '84422881') 

然而,當我把它包在一個CASE語句,如下

UPDATE [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST] 
SET [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST].[CustomerCLI] = 

CASE WHEN 

SELECT * 
FROM [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST] A 
WHERE EXISTS (SELECT 1 
       FROM [BillingReferenceData].[dbo].[CLIMapping] B 
       WHERE A.CustomerCLI = B.CLI AND A.CustomerCLI = '84422881'))>1 

THEN 'X' ELSE 'Y' END 

它失敗,出現以下錯誤

「只有一個表達式可以在選擇列表中指定當子查詢不與EXISTS一起引入「

+0

我猜它應該是'select count(*)'... Select *返回一個結果集,然後嘗試將它與一個標量值進行比較...... –

回答

0

使用」top 1「

UPDATE [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST] 
SET [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST].[CustomerCLI] = 

CASE WHEN 

SELECT top 1 * 
FROM [VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST] A 
WHERE EXISTS (SELECT 1 
       FROM [BillingReferenceData].[dbo].[CLIMapping] B 
       WHERE A.CustomerCLI = B.CLI AND A.CustomerCLI = '84422881'))>1 

THEN 'X' ELSE 'Y' END 
0

您正在試圖在[BillingReferenceData].[dbo].[CLIMapping]表中找到CustomerCLI的數字,並且想要用XY數值更新[VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST]表中的數值?

您需要在CASE聲明中使用SELECT COUNT(1)

相關問題