2011-12-15 149 views
-1
Alter Procedure sp_Member(
@comcode int = 0, 
@SubComCode int = 0 
) 
as 
begin 
set nocount on 
If @comcode= 0 
begin 
select 
(select sum(amount)  
    from tbcoudet 
    where memcode=tbm.memcode and expyear=(select max(expyear) from tbexpyear) 
    and exists(select itemcode 
      from tbitem 
      where [email protected] and 
        [email protected] and 
        itemcode=tbcoudet.itemcode 
      ) 
    group by memcode,expyear 
)'TurnOver' 
, * into #subodh from tbmember tbm where can_flag='N' 
end 
If @SubComCode= 0 
begin 
Insert into #subodh 
select 
(select sum(amount)  
    from tbcoudet 
    where memcode=tbm.memcode and expyear=(select max(expyear) from tbexpyear) 
    and exists(select itemcode 
      from tbitem 
      where [email protected] and 
        itemcode=tbcoudet.itemcode 
      ) 
    group by memcode,expyear 
)'TurnOver' 
, * from tbmember tbm where can_flag='N' 
end 

select top 10 * from #subodh where TurnOver is not null order by TurnOver desc 

end 

我的问题是,当我要去执行EXEC sp_Member @comcode='1',@subcomcode='1' OR select top 10 * from #subodh where TurnOver is not null order by TurnOver这些查询的话,我歌厅错误“无效的对象名称#subodh'。”请告诉我为什么我geting这个错误,我该如何解决它...请无效的对象名称#subodh“

回答

0

发生此错误,因为表可能不会创建与insert into声明。发生这种情况是因为select可能没有给出任何结果。所以编译器不允许这个代码,因为将来可能会给出错误。

而不是让一个insert into的,createtable #subodh第一(和drop它在结束),你不会得到这个问题。