declare @ZScore_ID  nvarchar(255),
@allZScore_ID  nvarchar(255),
@ZGift_ID  nvarchar(255),
@allZGift_ID  nvarchar(255),
@sumqtyvip  nvarchar(255),
@sumqtysvip  nvarchar(255),
 @sumqtyallvip  nvarchar(255),
 @ZVIP_Class nvarchar(255),
 @Gift nvarchar(255),
 @data nvarchar(255)
set @ZVIP_Class='全部'
set  @Gift='全部'
set @data='2004-01-09'select @ZScore_ID=ZScore_ID from ZSMemberInfo where ZVIP_Class=@ZVIP_Class)select @allZScore_ID=ZScore_ID from ZSMemberInfoselect  @ZGift_ID=ZGift_ID from ZSGiftInfo where ZGift_Desc=@Giftselect @allZGift_ID=ZGift_ID from ZSGiftInfo select @sumqtyvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='普通卡')and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and ZSGiftSignIn_d.ZScore_ID in (case when @ZVIP_Class='全部' then @allZScore_ID else @ZScore_ID end ) and ZSGiftSignIn_m.ZStart_Date = @data and ZSGiftSignIn_d.ZGift_ID in ( case when @Gift='全部' then @allZGift_ID else @ZGift_ID end)
select @sumqtysvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='白金卡')and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and ZSGiftSignIn_d.ZScore_ID in (case when @ZVIP_Class='全部' then @allZScore_ID else @ZScore_ID end )and ZSGiftSignIn_m.ZStart_Date = @data and ZSGiftSignIn_d.ZGift_ID in (case when @Gift='全部' then @allZGift_ID else @ZGift_ID  end)
select @sumqtyallvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category not in ('普通卡','白金卡'))and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and ZSGiftSignIn_d.ZScore_ID in (case when @ZVIP_Class='全部' then @allZScore_ID else @ZScore_ID end) and ZSGiftSignIn_m.ZStart_Date = @data and ZSGiftSignIn_d.ZGift_ID in (case when @Gift='全部' then @allZGift_ID else @ZGift_ID end) select @sumqtyvip,@sumqtysvip,@sumqtyallvip
GO

解决方案 »

  1.   

    建议将类似这样的语句
    set @var = (select ... from ...)
    改成
    select @var = ... from ...
      

  2.   

    set @allZScore_ID=(select ZScore_ID from ZSMemberInfo)
    set @allZGift_ID=(select ZGift_ID from ZSGiftInfo )这两个语句有问题。
    后面查询的结果不止一个。
    所以把值赋给@allZScore_ID、@allZGift_ID时有问题。把后面的SQL语句加上条件。或者改用游标。
      

  3.   

    declare @ZScore_ID  nvarchar(255),
    @allZScore_ID  nvarchar(255),
    @ZGift_ID  nvarchar(255),
    @allZGift_ID  nvarchar(255),
    @sumqtyvip  nvarchar(255),
    @sumqtysvip  nvarchar(255),
     @sumqtyallvip  nvarchar(255),
     @ZVIP_Class nvarchar(255),
     @Gift nvarchar(255),
     @data nvarchar(255)
    set @ZVIP_Class='全部'
    set  @Gift='全部'
    set @data='2004-01-09'select @sumqtyvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where 
    ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='普通卡') and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
    and ZSGiftSignIn_m.ZStart_Date = @data and
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when ZVIP_Class='全部' then ZGift_Desc else @Gift end)select @sumqtysvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='白金卡')and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
    and ZSGiftSignIn_m.ZStart_Date = @data and 
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when ZVIP_Class='全部' then ZGift_Desc else @Gift end)select @sumqtyallvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category not in ('普通卡','白金卡'))and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
     and ZSGiftSignIn_m.ZStart_Date = @data and 
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when ZVIP_Class='全部' then ZGift_Desc else @Gift end) select @sumqtyvip,@sumqtysvip,@sumqtyallvip
    GO
      

  4.   

    to 大力 查出的没有结果的呀。全部是null的
      

  5.   

    declare @ZScore_ID  nvarchar(255),
    @allZScore_ID  nvarchar(255),
    @ZGift_ID  nvarchar(255),
    @allZGift_ID  nvarchar(255),
    @sumqtyvip  nvarchar(255),
    @sumqtysvip  nvarchar(255),
     @sumqtyallvip  nvarchar(255),
     @ZVIP_Class nvarchar(255),
     @Gift nvarchar(255),
     @data nvarchar(255)
    set @ZVIP_Class='全部'
    set  @Gift='全部'
    set @data='2004-01-09'select @sumqtyvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where 
    ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='普通卡') and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when @ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
    and ZSGiftSignIn_m.ZStart_Date = @data and
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when @Gift='全部' then ZGift_Desc else @Gift end)select @sumqtysvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category='白金卡')and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when @ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
    and ZSGiftSignIn_m.ZStart_Date = @data and 
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when @Gift='全部' then ZGift_Desc else @Gift end)select @sumqtyallvip=sum(zqty) from ZSGiftSignIn_d,ZSGiftSignIn_m where ZSGiftSignIn_d.ZGift_ID in (select ZGift_ID from ZSGiftInfo where zGift_Category not in ('普通卡','白金卡'))and ZSGiftSignIn_m.ZVouch_ID=ZSGiftSignIn_d.ZVouch_ID and 
    exists(select 1 from ZSMemberInfo where ZScore_ID=ZSGiftSignIn_d.ZScore_ID and ZVIP_Class=case when @ZVIP_Class='全部' then ZVIP_Class else @ZVIP_Class end)
     and ZSGiftSignIn_m.ZStart_Date = @data and 
    exists(select 1 from ZSMemberInfo where ZGift_ID=ZSGiftSignIn_d.ZGift_ID and ZGift_Desc=case when @Gift='全部' then ZGift_Desc else @Gift end) select @sumqtyvip,@sumqtysvip,@sumqtyallvip
    GO