必须声明标量变量datetime

时间:2015-09-13 18:22:57

标签: sql sql-server

请帮帮我。为什么这段代码不起作用?说@FromDate必须声明标量变量

Alter Procedure rpt_1
as
    declare @FromDate Datetime 
    declare @ToDate Datetime
    declare @LocId int
Begin
    select 
        cl.CashLocationName, 
        convert(date,fn.DocumentDate) DocumentDate, 
        u.UserName,
        sum(TotalPayment) TotalVanzare, sum(CashAmount) TotalCash, 
        sum(CardAmount) TotalCard, sum(TicketPaymentAmount) TotalTichete, 
        sum(VATAmount)as TVA,
        count(FiscalNoteId) as NrBon, 
        round(sum(TotalPayment) / count(FiscalNoteId), 2) as ValoareMedieCos, 
        max(InternalTime) as MomentInchidere
    from 
        FiscalNote fn (nolock)
    Join 
        CashLocation cl (nolock) on cl.CashLocationId = fn.CashLocationId
    Join 
        ERPUser u (nolock) on u.UserId = fn.UserId  
    join 
        Site s (nolock) on s.SiteId = fn.SiteId
    where 
        DocumentDate >= @FromDate and DocumentDate <= @ToDate 
        and s.SiteId = @LocId  
        and fn.DocumentStateId = 36
    group by 
        cl.CashLocationName, fn.DocumentDate, OpenCashLocationMovementId, 
        u.UserName
    order by 
        DocumentDate,  CashLocationName, UserName
end

1 个答案:

答案 0 :(得分:1)

我假设您希望将这些变量作为参数,因为您不能将它们设置在任何位置。正确的语法是:

Alter Procedure rpt_1 (
    @FromDate Datetime,
    @ToDate Datetime,
    @LocId int
) as
Begin
    select 
        cl.CashLocationName, 
        convert(date,fn.DocumentDate) DocumentDate, 
        u.UserName,
        sum(TotalPayment) TotalVanzare, sum(CashAmount) TotalCash, 
        sum(CardAmount) TotalCard, sum(TicketPaymentAmount) TotalTichete, 
        sum(VATAmount)as TVA,
        count(FiscalNoteId) as NrBon, 
        round(sum(TotalPayment) / count(FiscalNoteId), 2) as ValoareMedieCos, 
        max(InternalTime) as MomentInchidere
    from 
        FiscalNote fn (nolock)
    Join 
        CashLocation cl (nolock) on cl.CashLocationId = fn.CashLocationId
    Join 
        ERPUser u (nolock) on u.UserId = fn.UserId  
    join 
        Site s (nolock) on s.SiteId = fn.SiteId
    where 
        DocumentDate >= @FromDate and DocumentDate <= @ToDate 
        and s.SiteId = @LocId  
        and fn.DocumentStateId = 36
    group by 
        cl.CashLocationName, fn.DocumentDate, OpenCashLocationMovementId, 
        u.UserName
    order by 
        DocumentDate,  CashLocationName, UserName
end;