日期:2014-05-17  浏览次数:20492 次

请问select查询语句如何调用存储过程?
如题:

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


------解决方案--------------------
非常遗憾告诉你存储过程这样不行,只能采用这种方式变通
insertt #TableName
exec pro_ProcedureName
------解决方案--------------------
好像不行,期待高手
建议你还是想怎么改函数来解决吧
------解决方案--------------------
函数的话可以直接使用。

如果是存储过程的话,可以用游标逐行循环数据。
------解决方案--------------------
SQL code

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,16

select * 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
end

go
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

------解决方案--------------------
openrowset
------解决方案--------------------
SQL code
SELECT  *
FROM    OPENROWSET('SQLOLEDB', 'SERVER=sss;uid=sa;pwd=123;Database=db ', 'exec sp') AS a