必须声明标量变量" @ PreviousAmount"

时间:2017-05-15 05:58:30

标签: 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"。"

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

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我没有遇到任何问题,只看这个例子。

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)

更新回答

根据您更新的问题,这里有两件事

  1. 首先,您还需要提供发票ID才能通过您的程序
  2. 您不能在select中使用多个结果的参数。
  3. 看看这个例子。这是在表格重新设计之上,需要了解样本数据:

    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
    

答案 2 :(得分:0)

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

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