mysql存储过程fetchinto
从存储过程返回表类型的值也有二种:
1.存储过程使用浮标参数,即同时指定CURSOR VARYING OUTPUT项.调用者可以使用while及fetch循环遍历该浮标.
2.直接将存储过程返回的结果集插入到表中,即使用insert into 表名 exec 存储过程.此种方式中注意存储过程返回的结果集列与insert的列要完全对应,可以在insert中指定列名来保证对应关系.
------------------------------------------------------------------------------测试:------------------------------------------------------------------------------
----建立测试用的临时表
create table #tmp (colx int,coly int)
insert into #tmp values(1,2)
insert into #tmp values(2,3)
insert into #tmp values(3,4)
select * from #tmpGO----创建返回游标的存储过程
create proc sp_c @cur CURSOR VARYING OUTPUTASbeginset @cur = CURSOR for select colx from #tmp
open @cur /*该过程返回游标,该游标为colx列的查询结果*/endGO----创建返回表的存储过程
create proc sp_dasselect coly from #tmp /*该过程返回coly列的查询结果*/go----创建用于调用以上二个存储过程的存储过程
create proc sp_easbegindeclare @x int
declare @cur cursor
----接收游标,并遍历游标
EXEC sp_c @cur OUTPUT
fetch next from @cur into @x
while (@@FETCH_STATUS = 0)beginprint @xfetch next from @cur into @xENDclose @curdeallocate @cur
----将存储过程返回的列值再重新插入源表中
❷ mysql中怎么定时调存储过程
那就用游标吧,代码没测试,建议先用测试数据测试一下能不能正常运行
delimiter//
createprocereCalcu_MonTotal()
begin
DECLAREv_datevarchar(16);
DECLAREv_totalint;
DECLAREv_tagnamevarchar(32);
SELECTDATE_FORMAT(DATE_ADD(NOW(),INTERVAL'-1'MONTH),'%Y-%m')intov_date;
DECLAREcursorc_Fetch
Forselecttagname,sum(daytotal)
FROMribao
WHEREddate>DATE_FORMAT(DATE_ADD(NOW(),INTERVAL'-1'MONTH),'%Y-%m')
andddate<DATE_FORMAT(NOW(),'%Y-%m')groupbytagname;
=1;
OPENc_Fetch;
REPEAT
FETCHc_FetchINTOv_tagname,v_total;
begin
insertintoyuetotal(tagname,ddate,yuetotal)values(v_tagname,v_date,v_total);
end;
UNTILstopFlag=1
ENDREPEAT;
CLOSEc_Fetch;
end
❸ mysql 存储过程
你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难
有看不懂的地方一起讨论 :
begin
declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;
declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if;
create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;
set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod order by ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;
end
❹ MySQL存储过程创建,我错在哪里了
你这个要取输入的ROWS以后的学生学号?
open cur;
FETCH cur into sno; 这里要用for循环啊。
不然你游标里面的数据很多,不可能都保存到sno里面的。
LOOP
FETCH cur into sno;
while FOUND DO
set ROWS=ROWS+1;
END LOOP
这样你的ROWS才会实现不停+1功能
❺ mysql存储过程游标结果集时,数据没有遍历完整
CREATEDEFINER=`root`@`%`PROCEDURE`insertPresale`()
BEGIN
#Routinebodygoeshere...
DECLAREdoneINTDEFAULT0;/*用于判断是否结束循环*/
DECLAREgoodsIdVARCHAR(255);#标记商品id
DECLAREflagINTDEFAULT0;#标记数据库是否包含此条商品记录
/*用于存储结果集的记录*/
/*定义游标*/
_idFROM`sys_goods_publish`WHEREpresale=1ANDpresale_time<=NOW();
/*定义设置循环结束标识done值怎么改变的逻辑*/
=1;/*done=true;亦可*/
OPENidCur;/*打开游标*/
/*循环开始*/
REPEAT
#/*如果要fetch多列应该这样写,fetchcur/*对应下面的idCur*/
FETCHidCurINTOgoodsId;/*还可以fetch多列(假设结果集的记录不是单列的话)*/
IFNOTdoneTHEN/*数值为非0,MySQL认为是true*/
SELECTCOUNT(*)INTOflagFROM`itemsinfonew`WHERETaoBaoitemId=goodsId;
IF(flag>0)THEN#如果数据库中有爬取此条记录则删除已用户发布的为准
DELETEFROM`itemsinfonew`WHERETaoBaoitemId=goodsId;
ENDIF;
INSERTINTO`itemsinfonew`(TaoBaoitemId,CouponID,CreateSourceName)
SELECTgoods_id,coupon_id,SUBSTRING("customer_fd",0.5)
FROM`sys_goods_publish`
WHEREpresale_time<=NOW()ANDgoods_id=goodsId;
UPDATE`sys_goods_publish`SETpresale=0WHEREpresale=1ANDpresale_time<=NOW()ANDgoods_id=goodsId;
ENDIF;
UNTILdoneENDREPEAT;
CLOSEidCur;/*关闭游标*/
❻ mysql 游标里fetch into存入的数据是全部还是一条
CREATE TABLE test_main (
id INT,
value VARCHAR(10),
PRIMARY KEY(id)
);
INSERT INTO test_main(id, value) VALUES (1, 'ONE');
INSERT INTO test_main(id, value) VALUES (2, 'TWO');
INSERT INTO test_main(id, value) VALUES (3, 'THREE');
DECLARE
-- 定义游标.
CURSOR c_test_main IS
SELECT id, value FROM test_main;
-- 保存游标数据的变量
v_main_data c_test_main%ROWTYPE;
BEGIN
-- 打开游标.
OPEN c_test_main;
LOOP
-- 填充数据(主表).
FETCH c_test_main INTO v_main_data;
-- 假如没有检索到(主表)数据,结束循环处理
Exit when c_test_main%NOTFOUND;
dbms_output.put_line(TO_CHAR(v_main_data.id)
|| ':' || v_main_data.value );
END LOOP;
-- 关闭游标
CLOSE c_test_main;
END;
❼ Mysql存储过程
set temp1=SELECT str_split(log,'"',1);
改成
set temp1=str_split(log,'"',1);
❽ mysql 存储过程 传多个参数无法识别
存储过程里的递归调用传的是一个参数,这肯定不对
while(done = 0) do
call iterateCategory(b);
fetch c into b;
end while;
❾ Mysql里如何使用存储过程
CREATE DEFINER 存储过程名
@变量1 类型 ----这里个就是传进来的
@变量2 类型 output ----这个是定义输出变量
as
declared @变量 类型,@变量 类型 --这里是定义内部变量 只能这是过程用
if 条件
begin
#¥%……&*
end
else
begin
#¥%……&
end
-----不知道你要什么
execute 数据库名.dbo.过程名 -----这个是 在这个过程里 调用别的过程