我有一个名为dbo.Movies的表,现在我想要更新3行
此声明仅适用于一条记录
declare @movietype nvarchar(100) = 'Thriller'
declare @price real = 10
if @price < (select Price from dbo.Movies where MovieType = @movietype)
begin
update mo
set mo.Price = Price - @price
FROM dbo.Movies as mo
WHERE mo.MovieType = @movietype
end
但是当我尝试运行此语句来更新这3行时出现错误
&#34; Msg 512,Level 16,State 1,Line 3 子查询返回的值超过1。当子查询遵循=,!=,&lt;,&lt; =,&gt;,&gt; =或子查询用作表达式时,不允许这样做。&#34;
如何修复此查询以更新3行或更多行?
答案 0 :(得分:3)
你的错误在于:
if @price < (select Price from dbo.Movies where MovieType = @movietype)
您无法将单个值(@price
)与多个值进行比较,您需要确保子查询只返回一个值。
另外,您也可以像这样重写命令:
UPDATE mo
SET mo.Price = Price - @price
FROM dbo.Movies as mo
WHERE mo.MovieType = @movietype
AND mo.Price > @Price