在UPDATE查询中使用select子句

时间:2017-03-04 07:50:35

标签: sql sql-server case

以下select语句可以正常工作。

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未引入子查询时,只能在选择列表中指定一个表达式”

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您正试图在CustomerCLI表中找到[BillingReferenceData].[dbo].[CLIMapping]号码,并希望在X表格中使用Y[VoiceflexBilling].[dbo].[FEBU17_OWH07579_Calls - TEST]值进行更新?

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