你不是写了用游标了吗?
有表test(a int,b varchar(10),c float)
declare @a int,@b varchar(10),@c float
declare c_tmp cursor for select a,b,c from test where 你的条件
open c_tmp
fetch next from c_tmp into @a,@b,@c
while (@@fetch_status<>-1)
begin
.....你的具体操作....
fetch next from c_tmp into @a,@b,@c
end
close c_tmp
deallocate c_tmp
有表test(a int,b varchar(10),c float)
declare @a int,@b varchar(10),@c float
declare c_tmp cursor for select a,b,c from test where 你的条件
open c_tmp
fetch next from c_tmp into @a,@b,@c
while (@@fetch_status<>-1)
begin
.....你的具体操作....
fetch next from c_tmp into @a,@b,@c
end
close c_tmp
deallocate c_tmp
我记得以前想做一个断号查询的问题,本也以为非用游标不可,后来铁版主开了个贴子,居然有N个用SQL就可以解决的办法,呵呵
declare cur_1 cursor for select code,times from child_mx where ic=:ls_ic group by code,times order by code;
open cur_1;
fetch cur_1 into :ls_code,:ls_times;
do while sqlca.sqlcode=0
select name into :ls_ymname from child_mx where code=:ls_code and jzbj='1';
select count(distinct ic) into :ls_y1 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:ld_begin_date and jzdate<=:ld_end_date and rs<>:ls_rs ;
select count(distinct ic) into :ls_y2 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:ld_begin_date and jzdate<=:ld_end_date and rs<>:ls_rs and jzbj='1';
select count(distinct ic) into :ls_y3 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:begin_date and jzdate<=:end_date and rs<>:ls_rs ;
select count(distinct ic) into :ls_y4 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:begin_date and jzdate<=:end_date and rs<>:ls_rs and jzbj='1';
select count(distinct ic) into :ls_y5 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:ld_begin_date and jzdate<=:ld_end_date and rs=:ls_rs ;
select count(distinct ic) into :ls_y6 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:ld_begin_date and jzdate<=:ld_end_date and rs=:ls_rs and jzbj='1';
select count(distinct ic) into :ls_y7 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:begin_date and jzdate<=:end_date and rs=:ls_rs ;
select count(distinct ic) into :ls_y8 from child_mx where code=:ls_code and times=:ls_times and jzdate>=:begin_date and jzdate<=:end_date and rs=:ls_rs and jzbj='1';
ls_ymjzqk=ls_ymname+" "+ls_times+"剂"
insert into child_df(ym,y1,y2,y3,y4,y5,y6,y7,y8)
values(:ls_ymjzqk,:ls_y1,:ls_y2,:ls_y3,:ls_y4,:ls_y5,:ls_y6,:ls_y7,:ls_y8);
fetch cur_1 into :ls_code,:ls_times;
loop
close cur_1;