我尝试在SQL Server
中编写公式,以减去两个列的值,这两列是table2的[AmountSpent]
列和两个不同表的table1的[Amount]
列,并更新余额金额[金额]专栏有什么想法吗?
提前谢谢你......
答案 0 :(得分:1)
我认为以下内容将起作用,假设您在两个表之间具有可靠的外键关系
UPDATE [table1]
SET [table1].[Amount] = [table1].[Amount] - [table2].[AmountSpent]
FROM
[table1] INNER JOIN
[table2] ON
[table1].[KeyField] = [table2].[KeyField]
答案 1 :(得分:0)
我明白了.........感谢@james Osborn
create procedure SP_Subtraction
(
@EmpID int
)
as
begin
UPDATE PTS_BalanceTracker
SET PTS_BalanceTracker.Balance_BalanceAmount = PTS_BalanceTracker.Balance_BalanceAmount - PTS_Transactions.Transaction_Amount
FROM
PTS_BalanceTracker INNER JOIN
PTS_Transactions ON
PTS_BalanceTracker.Emp_ID = @EmpID
end