Sql server 必须声明标量变量"@“以前的金额”;

Sql server 必须声明标量变量"@“以前的金额”;,sql-server,tsql,Sql Server,Tsql,这就是我所做的 Declare @PreviousAmount money set @PreviousAmount = 0 select @PreviousAmount = @PreviousAmount + Inp.AmountPaid From Invoice as Inv , InvoicePayment as Inp where Inv.InvoiceId = Inp.InvoiceId 我说了一个错误 必须声明标量变量“@PreviousAmount”

这就是我所做的

Declare @PreviousAmount money   
set @PreviousAmount = 0

select @PreviousAmount  = @PreviousAmount  + Inp.AmountPaid
    From Invoice as Inv , InvoicePayment as Inp
    where Inv.InvoiceId = Inp.InvoiceId 
我说了一个错误 必须声明标量变量“@PreviousAmount”

更新-这是给出错误的实际代码

CREATE PROCEDURE [dbo].[spInvoiceTsk]
        @AmountPaid money 
AS
BEGIN
    SET NOCOUNT ON;
    Declare @PreviousAmount money 
    set @PreviousAmount = 0

select case 
        when (@AmountPaid = Inv.InvoiceAmount )
         Then @AmountPaid 

        else (
        select @PreviousAmount  = @PreviousAmount  + Inp.AmountPaid
        From dbo.Invoice as Inv , dbo.InvoicePayment as Inp
        where Inv.InvoiceId = Inp.InvoiceId
        )
        from dbo.Invoice as Inv

        --select *
        --from Invoice as a
        --Inner Join InvoicePayment as IP ON a.InvoiceId = IP.InvoiceId

    update I
    set I.AmountPaid = @PreviousAmount
    from Invoice as I , InvoicePayment as IP
    where I.AmountPaid = IP.AmountPaid

END
GO

您的查询没有发现任何问题。提供更多详细信息或尝试执行以下查询

Declare @PreviousAmount money   
set @PreviousAmount = 0

set @PreviousAmount  = (Select @PreviousAmount  + Inp.AmountPaid
    From Invoice as Inv , InvoicePayment as Inp
    where Inv.InvoiceId = Inp.InvoiceId)

Select @PreviousAmount 

您的查询中没有任何问题,我的猜测是您在执行查询时离开了声明部分,因此请尝试一次运行所有部分。请按以下格式更改
加入

DECLARE @PreviousAmount MONEY   
SET @PreviousAmount = 0

SELECT @PreviousAmount  = @PreviousAmount  + Inp.AmountPaid
FROM Invoice AS Inv
INNER JOIN InvoicePayment AS Inp ON Inv.InvoiceId = Inp.InvoiceId

我没有得到任何问题,只是看看这个例子

Declare @PreviousAmount money   
set @PreviousAmount = 0

declare @invoice table(InvoiceId int, InvoiceDate datetime )
declare @InvoicePayment table(InvoiceId int, AmountPaid money )

insert into @invoice values (1, getdate()-365), (2, getdate()-30), (3, getdate()-3), (4, getdate()-1)
insert into @InvoicePayment values (1, 5) , (2, 30), (3, 3), (4, 100)

select @PreviousAmount  = @PreviousAmount  + Inp.AmountPaid
    From @Invoice as Inv , @InvoicePayment as Inp
    where Inv.InvoiceId = Inp.InvoiceId

select @PreviousAmount

--To verify the above answer
select sum(Inp.AmountPaid) totalamountOfAllInvoice
    From @Invoice as Inv , @InvoicePayment as Inp
    where Inv.InvoiceId = Inp.InvoiceId
两个查询都给出了138(5+30+3+100)

更新的答案

CREATE PROCEDURE [dbo].[spInvoiceTsk]
        @AmountPaid money 
AS
BEGIN
    SET NOCOUNT ON;
    Declare @PreviousAmount money 
    set @PreviousAmount = 0

select case 
        when (@AmountPaid = Inv.InvoiceAmount )
         Then @AmountPaid 

        else (
        select @PreviousAmount  = @PreviousAmount  + Inp.AmountPaid
        From dbo.Invoice as Inv , dbo.InvoicePayment as Inp
        where Inv.InvoiceId = Inp.InvoiceId
        )
        from dbo.Invoice as Inv

        --select *
        --from Invoice as a
        --Inner Join InvoicePayment as IP ON a.InvoiceId = IP.InvoiceId

    update I
    set I.AmountPaid = @PreviousAmount
    from Invoice as I , InvoicePayment as IP
    where I.AmountPaid = IP.AmountPaid

END
GO
根据你最新的问题,这里有两件事

  • 首先,您还必须需要发票id才能在您的过程中传递
  • 不能在“选择多个结果”中使用参数 看看这个例子。这是上表中的重新设计,其中包含示例数据,以了解:

    Declare @PreviousAmount money   
    set @PreviousAmount = 0
    
    declare @invoice table(InvoiceId int, InvoiceDate datetime, InvoiceAmount int )
    declare @InvoicePayment table(InvoiceId int, AmountPaid money )
    
    insert into @invoice values (1, getdate()-365 , 100), (2, getdate()-30 , 200), (3, getdate()-3 , 300), (4, getdate()-1 , 400)
    insert into @InvoicePayment values (1, 50) , (1, 20), (2, 200), (3, 50), (3, 100)
    
    
    Declare  @AmountPaid money = 10, @invoiceid int = 1
    
    Set @PreviousAmount =
        (Select 
            Case 
                When (@AmountPaid = Inv.InvoiceAmount ) Then @AmountPaid 
                Else (
                    select sum( Inp.AmountPaid) 
                    From 
                    @InvoicePayment as Inp where Inv.InvoiceId = Inp.InvoiceId              
                )
                end amountpaid
            from @Invoice as Inv 
            where inv.invoiceid = @invoiceid        
        )
    
    select @previousamount
    
    现在,基于此更新此过程

    CREATE PROCEDURE [dbo].[spInvoiceTsk]
            @AmountPaid money 
    AS
    BEGIN
        SET NOCOUNT ON;
        Declare @PreviousAmount money 
        set @PreviousAmount = 0
    
        --Select 
        --  Case 
        --      When (@AmountPaid = Inv.InvoiceAmount ) Then @AmountPaid 
        --      Else (
        --          select @PreviousAmount = @PreviousAmount  + Inp.AmountPaid
        --          From dbo.Invoice as Inv , dbo.InvoicePayment as Inp
        --          where Inv.InvoiceId = Inp.InvoiceId
        --      )
     --       from dbo.Invoice as Inv
    
            --select *
            --from Invoice as a
            --Inner Join InvoicePayment as IP ON a.InvoiceId = IP.InvoiceId
    
        /*Rather than above query, use left outer join and sum it to get previous amount of an invoice*/
        Set @PreviousAmount =
        (Select 
            Case 
                When (@AmountPaid = Inv.InvoiceAmount ) Then @AmountPaid 
                Else (
                    select sum( Inp.AmountPaid) 
                    From 
                    @InvoicePayment as Inp where Inv.InvoiceId = Inp.InvoiceId              
                )
                end amountpaid
            from @Invoice as Inv 
            where inv.invoiceid = @invoiceid        
        )
    
        update I
        set I.AmountPaid = @PreviousAmount
        from Invoice as I , InvoicePayment as IP
        where I.AmountPaid = IP.AmountPaid
    
    
    
    END
    GO
    

    您是否正在尝试创建已支付金额的累计总和?请提供一些示例数据和预期输出。就我个人而言,在运行存储过程创建脚本时,我发现“附近的语法不正确=”。您的SP离正常运行还有很长的路要走。中间的选择无效。您是否试图从付款表中分配部分或全部付款?你可能用一两个就可以做到updates@melina,我用你的答案更新你的问题。所以请删除你的答案。同时检查我的答案。好的。谢谢你,如果你的代码不起作用,我会尝试你的代码。我会问阿加尼你不明白。我只是说你,我没有得到你得到的任何错误。对于这个示例代码是用结果粘贴的,你能试试我上面发布的帖子吗?这是完整的代码,有一个错误你说“首先你还需要发票id才能在你的过程中传递”我不能传递InvoiceId作为参数,因为我有一个salesTransaction表,我需要的是在计算发票表后在salesTransaction表中标记InvoiceId我的意思是,你需要以前的金额,所以我的问题是,你们以前的金额是多少,根据所有的发票还是特定的发票。