楼主,这样解决吧。
Amount还是用Amount=isnull(Amount,0) If Exists(条件)
Begin
......
End
Else
  写一条在临时表#t中插入一个0值Amount的SQL语句

解决方案 »

  1.   

    ALTER PROCEDURE up_Accounts_GetBonusByUserID
    @UserID int
    as
        create table TempTable(UserID int,[level] int)
        declare @level int
        set @level=0
        insert TempTable select @UserID,@level
        while @@rowcount>0
        begin
        set @level=@level+1
        insert TempTable select Accounts_Users.UserID,@level
        from Accounts_Users,TempTable
        where Accounts_Users.FatherID=TempTable.UserID and TempTable.[level]=@level-1
        end    select id=identity(int,1,1),UserNo=cast(right((10000000+UserID),7) as varchar(10)),*,
        Ratio=case  when Point<301 then 0 
                    when Point>300 and Point<1001 then 0.03
                    when Point>1000 and Point<6001 then 0.06
                    when Point>6000 and Point<18001 then 0.09
                    when Point>18000 then 0.12
         end,Amount=isnull(Amount,0) --(case when Amount not exists then 0 else Amount end) 
         into #t
        from(
        select Accounts_Users.*,l=TempTable.[level]  
        from Accounts_Users
    join TempTable on Accounts_Users.UserID=TempTable.UserID and [level]<3
    left join Accounts_Expenditure on Accounts_Users.UserID=Accounts_Expenditure.UserID and ExpdMonth=month(getdate()) 
    )t
          update #t
          set ratio=case when l=0 then (select ratio from #t where id=1)-0
                         else (select ratio from #t where id=1)-Accounts_Users.ratio
                         end
          from #t Accounts_Users
          update #t
          set ratio=0.03 from #t where ratio=0
     
        select * from #t
        select sum(ratio*amount)/100
        from #t   
        
        drop table #t
        drop table TempTable
      

  2.   

    zjcxc(邹建) 您好:
    显示:Ado错误,列名 ' Amount' 无效。
      

  3.   

    Amount是Accounts_Expenditure中的列名,money类型,明明有啊,为什么显示:Ado错误,列名 ' Amount' 无效。
      

  4.   

    ALTER PROCEDURE up_Accounts_GetBonusByUserID
    @UserID int
    as
        create table TempTable(UserID int,[level] int)
        declare @level int
        set @level=0
        insert TempTable select @UserID,@level
        while @@rowcount>0
        begin
        set @level=@level+1
        insert TempTable select Accounts_Users.UserID,@level
        from Accounts_Users,TempTable
        where Accounts_Users.FatherID=TempTable.UserID and TempTable.[level]=@level-1
        end    select id=identity(int,1,1),UserNo=cast(right((10000000+UserID),7) as varchar(10)),*,
        Ratio=case  when Point<301 then 0 
                    when Point>300 and Point<1001 then 0.03
                    when Point>1000 and Point<6001 then 0.06
                    when Point>6000 and Point<18001 then 0.09
                    when Point>18000 then 0.12
         end,Amount=isnull(Amount,0) --(case when Amount not exists then 0 else Amount end) 
         into #t
        from(
        select Accounts_Users.*,l=TempTable.[level]  ,Amount
        from Accounts_Users
    join TempTable on Accounts_Users.UserID=TempTable.UserID and [level]<3
    left join Accounts_Expenditure on Accounts_Users.UserID=Accounts_Expenditure.UserID and ExpdMonth=month(getdate()) 
    )t
          update #t
          set ratio=case when l=0 then (select ratio from #t where id=1)-0
                         else (select ratio from #t where id=1)-Accounts_Users.ratio
                         end
          from #t Accounts_Users
          update #t
          set ratio=0.03 from #t where ratio=0
     
        select * from #t
        select sum(ratio*amount)/100
        from #t   
        
        drop table #t
        drop table TempTable
      

  5.   

    存储过程编译通过,但程序运行时有错误:System.Data.SqlClient.SqlException: 各表中的列名必须唯一。在表 '#t' 中多次指定了列名 'Amount'。
      

  6.   

    去掉into #t,检查列名是否重复
      

  7.   

    去掉 select id=identity(int,1,1),UserNo=cast(right((10000000+UserID),7) as varchar(10)),*,这句中的* 号就行了。
      

  8.   

    System.Data.SqlClient.SqlException: 列名 'l' 无效。这个 l是表示层次的,怎么会无效?
      

  9.   

    --要用l做为列名么?[l]=TempTable.[level]
      

  10.   

    怎么改成  [l]=TempTable.[level] 后还是不行?
      

  11.   

    不知道你原来的 写法: l=TempTable.[level] 
    是代表什么意思?
      

  12.   

    已经解决了。
    ALTER PROCEDURE up_Accounts_GetBonusByUserID
    @UserID int
    as
        create table #TempTable(UserID int,[level] int)
        declare @level int
        set @level=0
        insert #TempTable select @UserID,@level
        while @@rowcount>0
        begin
        set @level=@level+1
        insert #TempTable select Accounts_Users.UserID,@level
        from Accounts_Users,#TempTable
        where Accounts_Users.FatherID=#TempTable.UserID and #TempTable.[level]=@level-1
        end    select id=identity(int,1,1),UserNo=cast(right((10000000+UserID),7) as varchar(10)),TrueName,Point,
        Ratio=case  when Point<301 then 0 
                    when Point>300 and Point<1001 then 0.03
                    when Point>1000 and Point<6001 then 0.06
                    when Point>6000 and Point<18001 then 0.09
                    when Point>18000 then 0.12
         end,ILevel,Amount=isnull(Amount,0) --(case when Amount not exists then 0 else Amount end) 
         into #t
        from(
        select Accounts_Users.*,ILevel=#TempTable.[level]  ,Amount
        from Accounts_Users
    join #TempTable on Accounts_Users.UserID=#TempTable.UserID and [level]<3
    left join Accounts_Expenditure on Accounts_Users.UserID=Accounts_Expenditure.UserID and ExpdMonth=month(getdate()) 
    )t
          update #t
          set ratio=case when ILevel=0 then (select ratio from #t where id=1)-0
                         else (select ratio from #t where id=1)-Accounts_Users.ratio
                         end
          from #t Accounts_Users
          update #t
          set ratio=0.03 from #t where ratio=0
     
        select UserNo,TrueName,Point,Ratio,ILevel,Amount from #t
        select sum(ratio*amount)/100
        from #t   
        
        drop table #TempTable