SQL Server错误42000-必须声明标量变量

时间:2018-07-18 08:42:20

标签: sql sql-server

我目前在尝试运行SQL查询时遇到此错误:

  

[Err] 42000-[SQL Server]必须声明标量变量“ @StartDate”。
  42000-[SQL Server]必须声明标量变量“ @StartDate”。

我该如何解决?

这是我的完整查询:

declare  @StartDate DateTime = '2018-05-01';
declare @EndDate DateTime = '2018-05-31';

with t_Redemption as (
  select Customer, isNull(sum([v2_pointredeem]),0) [v2_pointredeem], isNull(sum([v5_Gift]),0) [v5_Gift]
  from (
    select isNull(c.[member id],'') as Customer,
           case when convert(date,r.[Redemption Date]) < Cast(@StartDate as date) then 
                  [TotalProductPoints]
                else 
                  0
           end as [v2_pointredeem],
           case when cast( r.[Redemption Date] as date) >= Cast( @StartDate as date) 
                     and cast( r.[Redemption Date] as date) <= Cast(@EndDate as date) then 
                  [TotalProductPoints]
                else 
                  0
           end as [v5_Gift]
    from QF_Redemption r
         left join qf_customer c on r.Customer = c.[ID]
  ) t
  group by Customer
),
t_Transaction as (
  select Customer, isNull(sum([v1]),0) [v1], isNull(sum([v4_SalesTransaction]),0) [v4_SalesTransaction], 
         isNull(sum([v3_ExpiredPoint]),0) [v3_ExpiredPoint], isNull(sum([v7_Expired]),0) [v7_Expired]
  from (
    select Customer,
           case when convert(date,[Transaction Date]) < Cast(@StartDate as date) then
                  (Cast(IsNull([ExtraPoints],0) as int)+Cast(IsNull([TotalPoints],0) as int)+Cast(IsNull([TotalMultiplier],0) as int))
                else 
                  0
           end as [v1],
           case when convert(date,[Transaction Date]) >= Cast(@StartDate as date) 
                     and convert(date,[Transaction Date]) <= Cast(@EndDate as date) then
                  (Cast(IsNull([ExtraPoints],0) as int)+Cast(IsNull([TotalPoints],0) as int)+Cast(IsNull([TotalMultiplier],0) as int))
                else 
                  0
           end as [v4_SalesTransaction],
           case when convert(date,[ValidityDate]) < Cast(@StartDate as date) then
                     -- When [ValidityDate] >= Cast(@StartDate as date) and [ValidityDate] <= Cast(@EndDate as date) then
                  ( Cast(IsNull([ExtraPoints],0) as int)+
                     Cast(IsNull([TotalPoints],0) as int)+
                     Cast(IsNull([TotalMultiplier],0) as int)-
                     Cast(IsNull([TotalPointsRedeemed], 0) as int))
                else 
                  0
           end as [v3_ExpiredPoint],
           case when convert(date,[ValidityDate]) >= Cast(@StartDate as date) 
                     and convert(date,[ValidityDate]) <= Cast(@EndDate as date) then 
                  ( Cast(IsNull([ExtraPoints],0) as int)+
                     Cast(IsNull([TotalPoints],0) as int)+
                     Cast(IsNull([TotalMultiplier],0) as int)-
                     Cast(IsNull([TotalPointsRedeemed], 0) as int))
                else 
                  0
           end as [v7_Expired]
  from QF_Transaction
  --where outlet = @Outlet
  ) t
  group by Customer
)

Select  'c' + customer , sum(v1)-sum(v2_pointredeem) - sum(v3_ExpiredPoint) as OB, sum(v4_SalesTransaction) SalesTransaction,sum(v5_Gift) Gift_Product,sum(v7_Expired) Expired 
from (
  select customer,v1, 0 v2_pointredeem,   v3_ExpiredPoint,   v4_SalesTransaction, 
         0 v5_Gift,   v7_Expired
  from t_Transaction

  union all

  select customer,0 v1, v2_pointredeem, 0 v3_ExpiredPoint, 0 v4_SalesTransaction,  v5_Gift, 0 v7_Expired
  from t_Redemption
) ttt
 --Where v4_SalesTransaction> 0 or v5_Gift> 0 or v7_Expired > 0
group by customer 

非常感谢您为解决这个问题提供的帮助。

干杯, 利哈多

1 个答案:

答案 0 :(得分:1)

由于已经声明了@StartDate变量,因此完整脚本中必须包含一些GO语句。

您必须删除它们(或在GO行之后重新声明变量),因为它们会终止先前声明的变量的范围。