SQL Server错误42000 - 必须声明标量变量[关闭]

问题描述 投票:-4回答:1

我在尝试运行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 

我非常感谢您在解决这个问题时能给我的任何帮助。

干杯,利哈多

sql sql-server
1个回答
1
投票

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

你必须删除它们(或在GO行之后重新声明你的变量),因为它们结束了你先前声明的变量的范围。

© www.soinside.com 2019 - 2024. All rights reserved.