An aggregate may not appear in the set list of an UPDATE statement. If update with Column Names

StackOverflow https://stackoverflow.com/questions/23672401

  •  23-07-2023
  •  | 
  •  

質問

On run, gives error

An aggregate may not appear in the set list of an UPDATE statement.

Code:

 update CheckInCheckOut 
 set CountHours = (select ROUND(SUM(DATEDIFF(ss,CheckIn,CheckOut) / 60.0 / 60.0), 2))  
 where UserId_Fk = 3

This query works when I write it as:

update CheckInCheckOut 
set CountHours = ROUND(SUM(DATEDIFF(ss, '2014-05-15 00:23:04.580', '2014-05-15 00:25:04.580')  / 60.0 / 60.0), 2)

   CheckIn datetime,
  CheckOut datetime

Example would be appreciated.

役に立ちましたか?

解決

Try declare a variable for the purpose:

DECLARE @Result Decimal(18, 0)
SET @Result = ROUND(SUM(DATEDIFF(ss,'2014-05-15 00:23:04.580','2014-05-15 00:25:04.580') / 60.0 / 60.0), 2)

update CheckInCheckOut 
set CountHours= @Result
where UserId_Fk=3
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top