日期:2014-05-17 浏览次数:20640 次
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
------解决方案--------------------
SELECT  *
FROM    OPENROWSET('SQLOLEDB', 'SERVER=sss;uid=sa;pwd=123;Database=db ', 'exec sp') AS a