如题:table1表:字段a1,a2,a3
存储过程Pro1 a1,a2,a3(本来是函数,可以实现,但由于查询表不确定要实现动态执行,所以只能改成存储过程)
变量@count
select @count=sum(pro1 a1,a2,a3) from table1
我要实现这样的,请问如何实现?

解决方案 »

  1.   

    非常遗憾告诉你存储过程这样不行,只能采用这种方式变通
    insertt #TableName
    exec pro_ProcedureName  
      

  2.   

    请把你的存储过程再介绍一下,“存储过程Pro1 a1,a2,a3”这句话什么意思,是一个存储过程,还是三个存储过程,若是三个存储过程,每个存储过程的变量分别是什么?
      

  3.   

    存储过程名称Pro1,需要传递的参数是a1,a2,a3。存储过程中需要查询的表是不确定的,根据a1(a1为ID)组合成表名,再用exec sp_executesql查询获取结果。
    table1表的字段a1,a2,a3分别对应存储过程Pro1的三个参数。
      

  4.   


    create table table1(a1 int ,a2 int,a3 int)
    insert table1
    select 1,3,4 union all
    select 2,3,4 union all
    select 6,7,8 union all
    select 9,1,6 union all
    select 12,13,16select * from table1
    /*
    a1          a2          a3
    ----------- ----------- -----------
    1           3           4
    2           3           4
    6           7           8
    9           1           6
    12          13          16
    */go
    --随便写个存储过程
    create proc Proc_table1 
    (@a1 int ,@a2 int, @a3 int)
    as
    begin
    select 2*@a1+3*@a2+@a3
    endgo
    create table #t(asum int)
    declare my_cursor cursor for
    select a1,a2,a3 from table1
    open my_cursor
    declare @a1 int, @a2 int,@a3 int 
    fetch next from my_cursor into @a1,@a2,@a3
    while(@@fetch_status=0)
      begin
        insert into #t exec Proc_table1 @a1,@a2,@a3 --执行存储过程
        fetch next from my_cursor into @a1,@a2,@a3
      end
    close my_cursor
    deallocate my_cursor
    select sum(asum) from #t
    /*
    179
    */
    drop table #t
      

  5.   

    select @count=sum(pro1 a1,a2,a3) from table1
    这一句实现什么功能?
      

  6.   

    SELECT  *
    FROM    OPENROWSET('SQLOLEDB', 'SERVER=sss;uid=sa;pwd=123;Database=db ', 'exec sp') AS a     
      

  7.   


    通过存储过程pro1获取每行的数据,比如求和,再把此列的所有行数据相加赋值给变量@count
      

  8.   


    这个好像不能实现我这个要求吧。看来也只能用maco_wang说的这种方法了。