我尝试更新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]
)
答案 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
值时,可以连续使用其他列。