两张表a和b,a表中一number型字段C,b表中两个number型字段D和E
求一函数计算这三个字段的和,a和b表通过ID关联,传入参数a表的ID

解决方案 »

  1.   


    declare @id int
    set @id = ???select sum(a.c)+sum(b.d+b.e)
    from a,b
    where a.id = b.id and a.id = @id--怎么还用函数呢?
      

  2.   


    update a set a=b.D+b.E from a left join b on a.ID=b.ID
      

  3.   

    sekect a.c + b.d + b.e from a , b where a.id = b.id
      

  4.   

    现在问题就是函数是准备用于视图中,视图是通过查询A表中数据返回结果,如果这样写的话,返回的结果可能是多行的,通过函数传入参数ID的话为了防止这一问题
      

  5.   


    create function get_sum(@id int)
    returns int
    as
    declare @sum int
    select @sum = sum(a.c)+sum(b.d+b.e)
    from a,b
    where a.id = b.id and a.id = @id
    return @sum
    end
    go
      

  6.   

    create function dbo.f_test (@id int)
    returns table
    as
    return
    select a.c+b.d+b.e from a,b
    where a.id=b.id
    and ID=@id
      

  7.   

    create view sumtable
    as 
      select a.ID, isnull(a.C,0)+isnull(b.D,0)+isnull(b.E,0) as sums 
      from  a left join b on a.ID=b.ID
    go