如题:table1表:字段a1,a2,a3
存储过程Pro1 a1,a2,a3(本来是函数,可以实现,但由于查询表不确定要实现动态执行,所以只能改成存储过程)
变量@count
select @count=sum(pro1 a1,a2,a3) from table1
我要实现这样的,请问如何实现?
存储过程Pro1 a1,a2,a3(本来是函数,可以实现,但由于查询表不确定要实现动态执行,所以只能改成存储过程)
变量@count
select @count=sum(pro1 a1,a2,a3) from table1
我要实现这样的,请问如何实现?
insertt #TableName
exec pro_ProcedureName
table1表的字段a1,a2,a3分别对应存储过程Pro1的三个参数。
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
这一句实现什么功能?
FROM OPENROWSET('SQLOLEDB', 'SERVER=sss;uid=sa;pwd=123;Database=db ', 'exec sp') AS a
通过存储过程pro1获取每行的数据,比如求和,再把此列的所有行数据相加赋值给变量@count
这个好像不能实现我这个要求吧。看来也只能用maco_wang说的这种方法了。