求和一列并将其更新到我的表格中?

时间:2011-07-19 19:08:18

标签: sql sql-server

DECLARE  @cnt int
select @cnt = 2

SUM工作正常!

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

但没有数据更新到表,@ cnt = 0?

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

1 个答案:

答案 0 :(得分:1)

@cntcnt是两回事。一个是服务器端变量,一个是表字段。 MySQL中完全不同的存储区域。如果您希望查询更新VARIABLE,那么您需要执行

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