如何从表中减去两个列值,从存储过程中减去另一个值

时间:2016-06-03 15:45:11

标签: sql-server

select C_Id , CD.Sd_Id, BU_Id ,CB.SpendPriority ,BudgetAmount ServiceBudgetAmount
from ContractBudget CB
    JOIN ContractDetail CD ON CB.Id = CD.CB_Id
    WHERE CB.C_Id =367 AND CD.Sd_Id = 2239

返回以下记录。

C_Id    Sd_Id   BU_Id   SpendPriority   ServiceBudgetAmount
367 2239    220 Normal  100000.00

我也有一个存储过程

DECLARE @return_value int

EXEC    @return_value = [dbo].[PaymentServiceDetailBusinessUnitAllocation]
        @MRRId = 592,
        @SDId = 2239

SELECT  'Return Value' = @return_value

GO

返回以下结果

BusinessUnit    E1Description   Fund    SpendPriority   ServiceType TotalPaymentAmount
12341232    created for Ram-MH1 CF  Normal  MH  220.00

任何人都可以帮助我如何计算价值 servicebudgetamount - totalpaymentamount

我需要结果

availableamount= 99780

2 个答案:

答案 0 :(得分:0)

你需要saclar valued function,而不是存储过程,它会是这样的

declare @ServiceBudgetAmount int
declare @TotalPaymentAmount int

select  @ServiceBudgetAmount = BudgetAmount
    from ContractBudget CB
    JOIN ContractDetail CD ON CB.Id = CD.CB_Id
    WHERE CB.C_Id =367 AND CD.Sd_Id = 2239

    set @TotalPaymentAmount = YourNewFunction ( 592, 2239);

    select @ServiceBudgetAmount - @TotalPaymentAmount  as Result

答案 1 :(得分:0)

您可以使用与sp输出相同的列(名称/数据类型)创建/声明临时表。然后做这样的事情:

INSERT INTO temp_table
EXEC your_stored_proc...

然后你可以比较你需要的东西。