使用临时表:
DECLARE @Results TABLE (
CDA varchar(60),
Found int default 0,
Accepted int default 0,
Percentage decimal(3,0) default 0.0,
)
如何获取已填充的Found和Accepted列并将其写回Pecentage列?
我有:
UPDATE @Results SET Percentage = (
SELECT (((Accepted * 1.00) / Found) * 100) FROM @Results
)
如果你选择SELECT行(注释掉UPDATE),它会返回正确的值。但是在UPDATE的上下文中,它以Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.
如何将值写回正确的行?
答案 0 :(得分:1)
你需要一个where子句,例如(另)
UPDATE Result oResult
SET Percentage = (SELECT Accepted / Found * 100
FROM Result iResult
WHERE iResult.cda = oResult.cda);
但是,如果这是计算出来的,您可能希望不将其作为列添加,而只是将其添加到任何查询中。请注意,大多数数据库都具有百分比功能。
答案 1 :(得分:1)
UPDATE Result
SET Percentage = Accepted / Found * 100
虽然将FOUND和ACCEPTED定义为INT,但在分割时使用强制转换或使用与PERCENTAGE相同的类型声明它们将无效。