SQL Server:错误“只能在选择列表中指定一个表达式.....”

时间:2014-11-28 13:47:00

标签: sql sql-server subquery

我尝试更新SQL Server中的表,以便使用下面的case语句更新列AR Classification。它被卡住了,并且只能指定一个表达式。有谁知道出了什么问题?

代码:

UPDATE [dbo].[FullBase]
SET [AR Classification] = 
    (
    SELECT *  
    ,CASE  
        WHEN [Revenue Type] = 'Recurring Charge' THEN 'Recurring - Spread over Invoice Period'  
        WHEN [Financial Product   Service Type] = 'Dark Fibre Service' AND ([Initial Term (Service)]   IS NULL OR [Initial Term (Service)] = 0 OR [Initial Term (Service)] = 1 OR [Initial Term (Service)]   >= 180) THEN 'One off - IRU Recognised at Delivery'    
        WHEN [Financial Product   Service Type] = 'Duct Service' AND ([Initial Term (Service)] IS NULL OR [Initial Term (Service)] = 0 OR [Initial Term (Service)] = 1 OR [Initial Term (Service)] >= 180) THEN 'One off - IRU Recognised at Delivery'  
        ELSE 'IRU - Deferred'
     END
     FROM [dbo].[FullBase]
    )

1 个答案:

答案 0 :(得分:1)

您的子查询使用与update表相同的表。这很不寻常。在这种情况下,我根本不认为你想要一个子查询。这可能是你想要的:

UPDATE [dbo].[FullBase]
    SET [AR Classification] = (CASE  WHEN [Revenue Type] = 'Recurring Charge' THEN 'Recurring - Spread over Invoice Period'  
                                     WHEN [Financial Product   Service Type] = 'Dark Fibre Service' AND ([Initial Term (Service)]   IS NULL OR [Initial Term (Service)] = 0 OR [Initial Term (Service)] = 1 OR [Initial Term (Service)]   >= 180) THEN 'One off - IRU Recognised at Delivery'    
                                     WHEN [Financial Product   Service Type] = 'Duct Service' AND ([Initial Term (Service)] IS NULL OR [Initial Term (Service)] = 0 OR [Initial Term (Service)] = 1 OR [Initial Term (Service)] >= 180) THEN 'One off - IRU Recognised at Delivery'  
                                     ELSE 'IRU - Deferred'   
                               END);

编辑:

你的问题涉及一些名为"标量子查询"。这些是在预期单个列值或常量时在上下文中使用的子查询。在这种情况下,您要在update中设置一个值,因此set需要一个值。

您的查询返回了多个列(*),也可能返回了多行。其中每一个都会产生错误。

我认为原因是对如何使用update的误解。设置update值时,可以连续使用其他列。