Sql:不同列的减法公式?

时间:2012-02-22 08:33:08

标签: sql-server-2008 sql-server-2005 c#-4.0

我尝试在SQL Server中编写公式,以减去两个列的值,这两列是table2的[AmountSpent]列和两个不同表的table1的[Amount]列,并更新余额金额[金额]专栏有什么想法吗?

提前谢谢你......

2 个答案:

答案 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