user823893
user823893

Reputation: 29

Sum a column and update it to my table?

DECLARE  @cnt int
select @cnt = 2

the SUM works fine!

SELECT SUM(Count) AS cnt FROM VF_CasINV_Cost
where (K = 'K') and (CalendarYear = 2010) AND (Item# < 99999992)

But no data is updated to the table, @cnt = 0 ?

UPDATE VF_CasINV_Cost
SET [Count] = @cnt
WHERE (K = 'K') and (CalendarYear = 2010) AND (Item# = 99999992)

Upvotes: 0

Views: 72

Answers (1)

Marc B
Marc B

Reputation: 360702

@cnt and cnt are two different things. one's a server-side variable, one's a table field. Completely different storage areas in MySQL. If you want the query to update the VARIABLE, then you'd need to do

SELECT @cnt := SUM(Count) AS cnt 
...

Upvotes: 1

Related Questions