我正在尝试在SQL Server 2012中执行两行代码:
SELECT SUM(paid) AS sumpaid FROM clipaid WHERE event='010101-01' AND year=2014;
UPDATE event SET totalpaid = sumpaid WHERE event='010101-01' AND year=2014
在第一行中,我想声明变量sumpaid
并将此变量更新为第二个statemnt中的参数。
我收到此错误:Invalid column name 'sumpaid'
有没有办法实现这个目标?
感谢您的时间和建议。
答案 0 :(得分:3)
您需要声明一个变量
declare @sumpaid int
SELECT @sumpaid = SUM(paid) FROM clipaid WHERE event='010101-01' AND year=2014;
UPDATE event SET totalpaid = @sumpaid WHERE event='010101-01' AND year=2014
答案 1 :(得分:1)
您需要声明一个变量,如下所示:
declare @varSumPaid int
并像这样分配它的值:
SELECT @varSumPaid = SUM(paid)
FROM clipaid
WHERE event='010101-01'
AND year=2014;
并在更新查询中使用它:
UPDATE event
SET totalpaid = @varSumPaid
WHERE event='010101-01'
AND year=2014;