当前位置:首页 » 存储配置 » mysql存储过程break

mysql存储过程break

发布时间: 2023-06-08 14:43:13

㈠ Mysql 存储过程遍历游标出错

MySql 存储过程遍历游标出错
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET DONE = 1;
这种语句是异常捕获内容,在mysql中都是使用HANDLER 来捕获异常的。

建议你看下:MySQL存储过程详解
MySQL游标循环遍历的使用

㈡ Mysql 存储过程中如何判断Cursor中结果集是否为空

0 通过定义一个上下文管理者(即declare continue handler)来实现
必须在游标定义后定义,并通过使用一个辅助变量来进行判断。

1 示例如下:

delimiter $
drop procere if exists curdemo $
CREATE PROCEDURE curdemo(pid int)
BEGIN
DECLARE notfound INT DEFAULT 0; #定义一个辅助变量用于判断
DECLARE a int; #定义游标输出值赋予的变量
DECLARE cur1 CURSOR FOR SELECT id FROM test.t where id= pid; #定义游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET notfound = 1; #定义declare continue handler,这个会根据上下文是否有结果判断是否执行SET notfound = 1

OPEN cur1;
FETCH cur1 INTO a;
if notfound = 1 then
select 'no result';
#写业务逻辑
ELSE
select concat('result:', a);
#写业务逻辑
end if;
CLOSE cur1;
END
$
delimiter ;

call curdemo(240);

㈢ mysql 使用存储过程 循环查找数据

delimiter
$$
mysql>
mysql>
CREATE
PROCEDURE
myProc()
//创建while循环的存储过程
if分支语句示例
->
BEGIN
->
->
DECLARE
i
int;
->
SET
i=1;
->
loop1:
WHILE
i<=10
DO
->
IF
MOD(i,2)<>0
THEN
/*Even
number
-
try
again*/
->
SELECT
CONCAT(i,"
is
an
odd
number");
->
END
IF;
->
SET
i=i+1;
->
END
WHILE
loop1;
->
END$$
Query
OK,
0
rows
affected
(0.00
sec)
这种也可以

㈣ mysql 存储过程

.关于MySQL的存储过程

存储过程是数据库存储的一个重要的功能,但是MySQL在5.0以前并不支持存储过程,这使得MySQL在应用上大打折扣。好在MySQL 5.0终于开始已经支持存储过程,这样即可以大大提高数据库的处理速度,同时也可以提高数据库编程的灵活性。

MySQL存储过程的创建

(1).格式

MySQL存储过程创建的格式:CREATE PROCEDURE过程名([过程参数[,...]])
[特性...]过程体

这里先举个例子:

  • mysql>DELIMITER//

  • mysql>CREATEPROCEDUREproc1(OUTsint)

  • ->BEGIN

  • ->SELECTCOUNT(*)INTOsFROMuser;

  • ->END

  • ->//

  • mysql>DELIMITER;


  • 注:
  • (1)这里需要注意的是DELIMITER //和DELIMITER ;两句,DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。

    (2)存储过程根据需要可能会有输入、输出、输入输出参数,这里有一个输出参数s,类型是int型,如果有多个参数用","分割开。

    (3)过程体的开始与结束使用BEGIN与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实现创建存储过程并循环添加记录的方法

本文实例讲述了MySQL实现创建存储过程并循环添加记录的方法。分享给大家供大家参考,具体如下:
先创建,然后调用:
--
创建存储过程
DELIMITER;//
create
procere
myproc()
begin
declare
num
int;
set
num=1;
while
num
<=
24
do
insert
into
t_calendar_hour(hourlist)
values(num);
set
num=num+1;
end
while;
commit;
end;//
--
调用存储过程
CALL
myproc();
PS:这里再为大家推荐2款常用的sql在线工具供大家参考使用(包含SQL语句用法说明):
SQL在线压缩/格式化工具:
http://tools.jb51.net/code/sql_format_compress
SQL代码在线格式化美化工具:
http://tools.jb51.net/code/sqlcodeformat
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL存储过程技巧大全》、《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。

㈦ mysql存储过程报错 找的快哭了...

加一个事务
多加几个判断就可以处理存储过程中的异常了,请采纳!

㈧ mysql存储过程 in 怎么用

out 表示输出的参数,存储过程调用 代码 需要获得此参数值。
in 表示输入参数,默认为in

例1、一个简单存储过程游标实例

复制代码代码如下:

DELIMITER $$
DROP PROCEDURE IF EXISTS getUserInfo $$
CREATE PROCEDURE getUserInfo(in date_day datetime)
--
-- 实例
-- 存储过程名为:getUserInfo
-- 参数为:date_day日期格式:2008-03-08
--
BEGIN
declare _userName varchar(12); -- 用户名
declare _chinese int ; -- 语文
declare _math int ; -- 数学
declare done int;
-- 定义游标
DECLARE rs_cursor CURSOR FOR SELECT username,chinese,math from userInfo where datediff(createDate, date_day)=0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
-- 获取昨天的日期
if date_day is null then
set date_day = date_add(now(),interval -1 day);
end if;
open rs_cursor;
cursor_loop:loop
FETCH rs_cursor into _userName, _chinese, _math; -- 取数据

if done=1 then
leave cursor_loop;
end if;
-- 更新表
update infoSum set total=_chinese+_math where UserName=_userName;
end loop cursor_loop;
close rs_cursor;

END$$
DELIMITER ;

例2、存储过程游标循环跳出现
在MySQL的存储过程中,游标操作时,需要执行一个conitnue的操作.众所周知,MySQL中的游标循环操作常用的有三种,LOOP,REPEAT,WHILE.三种循环,方式大同小异.以前从没用过,所以记下来,方便以后查阅.
1.REPEAT

复制代码代码如下:

REPEAT
Statements;
UNTIL expression
END REPEAT
demo
DECLARE num INT;
DECLARE my_string VARCHAR(255);
REPEAT
SET my_string =CONCAT(my_string,num,',');
SET num = num +1;
UNTIL num <5
END REPEAT;

2.WHILE

复制代码代码如下:

WHILE expression DO
Statements;
END WHILE
demo
DECLARE num INT;
DECLARE my_string VARCHAR(255);
SET num =1;
SET str ='';
WHILE num < span>10DO
SET my_string =CONCAT(my_string,num,',');
SET num = num +1;
END WHILE;
3.LOOP(这里面有非常重要的ITERATE,LEAVE)
代码如下 复制代码
DECLARE num INT;
DECLARE str VARCHAR(255);
SET num =1;
SET my_string ='';
loop_label: LOOP
IF num <10THEN
LEAVE loop_label;
ENDIF;
SET num = num +1;
IF(num mod3)THEN
ITERATE loop_label;
ELSE
SET my_string =CONCAT(my_string,num,',');
ENDIF;
END LOOP;

PS:可以这样理解ITERATE就是我们程序中常用的contiune,而ITERATE就是break.当然在MySQL存储过程,需要循环结构有个名称,其他都是一样的.
例3,mysql 存储过程中使用多游标

先创建一张表,插入一些测试数据:

复制代码代码如下:

DROP TABLE IF EXISTS netingcn_proc_test;
CREATE TABLE `netingcn_proc_test` (
`id` INTEGER(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20),
`password` VARCHAR(20),
PRIMARY KEY (`id`)
)ENGINE=InnoDB;
insert into netingcn_proc_test(name, password) values
('procere1', 'pass1'),
('procere2', 'pass2'),
('procere3', 'pass3'),
('procere4', 'pass4');下面就是一个简单存储过程的例子:
drop procere IF EXISTS test_proc;
delimiter //
create procere test_proc()
begin
-- 声明一个标志done, 用来判断游标是否遍历完成
DECLARE done INT DEFAULT 0;
-- 声明一个变量,用来存放从游标中提取的数据
-- 特别注意这里的名字不能与由游标中使用的列明相同,否则得到的数据都是NULL
DECLARE tname varchar(50) DEFAULT NULL;
DECLARE tpass varchar(50) DEFAULT NULL;
-- 声明游标对应的 SQL 语句
DECLARE cur CURSOR FOR
select name, password from netingcn_proc_test;
-- 在游标循环到最后会将 done 设置为 1
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
-- 执行查询
open cur;
-- 遍历游标每一行
REPEAT
-- 把一行的信息存放在对应的变量中
FETCH cur INTO tname, tpass;
if not done then
-- 这里就可以使用 tname, tpass 对应的信息了
select tname, tpass;
end if;
UNTIL done END REPEAT;
CLOSE cur;
end
//
delimiter ;
-- 执行存储过程
call test_proc();

需要注意的是变量的声明、游标的声明和HANDLER声明的顺序不能搞错,必须是先声明变量,再申明游标,最后声明HANDLER。上述存储过程的例子中只使用了一个游标,那么如果要使用两个或者更多游标怎么办,其实很简单,可以这么说,一个怎么用两个就是怎么用的。例子如下:

复制代码代码如下:

drop procere IF EXISTS test_proc_1;
delimiter //
create procere test_proc_1()
begin
DECLARE done INT DEFAULT 0;
DECLARE tid int(11) DEFAULT 0;
DECLARE tname varchar(50) DEFAULT NULL;
DECLARE tpass varchar(50) DEFAULT NULL;
DECLARE cur_1 CURSOR FOR
select name, password from netingcn_proc_test;
DECLARE cur_2 CURSOR FOR
select id, name from netingcn_proc_test;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur_1;
REPEAT
FETCH cur_1 INTO tname, tpass;
if not done then
select tname, tpass;
end if;
UNTIL done END REPEAT;
CLOSE cur_1;
-- 注意这里,一定要重置done的值为 0
set done = 0;
open cur_2;
REPEAT
FETCH cur_2 INTO tid, tname;
if not done then
select tid, tname;
end if;
UNTIL done END REPEAT;
CLOSE cur_2;
end
//
delimiter ;
call test_proc_1();

上述代码和第一个例子中基本一样,就是多了一个游标声明和遍历游标。这里需要注意的是,在遍历第二个游标前使用了set done = 0,因为当第一个游标遍历玩后其值被handler设置为1了,如果不用set把它设置为 0 ,那么第二个游标就不会遍历了。当然好习惯是在每个打开游标的操作前都用该语句,确保游标能真正遍历。当然还可以使用begin语句块嵌套的方式来处理多个游标,例如:

复制代码代码如下:

drop procere IF EXISTS test_proc_2;
delimiter //
create procere test_proc_2()
begin
DECLARE done INT DEFAULT 0;
DECLARE tname varchar(50) DEFAULT NULL;
DECLARE tpass varchar(50) DEFAULT NULL;
DECLARE cur_1 CURSOR FOR
select name, password from netingcn_proc_test;
DECLARE cur_2 CURSOR FOR
select id, name from netingcn_proc_test;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur_1;
REPEAT
FETCH cur_1 INTO tname, tpass;
if not done then
select tname, tpass;
end if;
UNTIL done END REPEAT;
CLOSE cur_1;
begin
DECLARE done INT DEFAULT 0;
DECLARE tid int(11) DEFAULT 0;
DECLARE tname varchar(50) DEFAULT NULL;
DECLARE cur_2 CURSOR FOR
select id, name from netingcn_proc_test;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
open cur_2;
REPEAT
FETCH cur_2 INTO tid, tname;
if not done then
select tid, tname;
end if;
UNTIL done END REPEAT;
CLOSE cur_2;
end;
end
//
delimiter ;
call test_proc_2();

㈨ 存储过程(mysql),如何跳出循环

恰当的使用IF语句,并在需要跳出循环时,将xxx设置成一个不可满足的条件,那么到下一次循环时,自然不会再执行.

㈩ MySQL如何终止存储过程执行

php调用mysql存储过程和函数的方法
存储过程和函数是MySql5.0刚刚引入的。关于这方面的操作在PHP里面没有直接的支持。但是由于Mysql
PHP
API的设计,使得我们可以在以前的PHP版本中的mysql
php
api中支持存储过程和函数的调用。
在php中调用存储过程和函数。
1。调用存储过程的方法。
a。如果存储过程有
IN/INOUT参数,声明一个变量,输入参数给存储过程,该变量是一对,
一个php变量(也可以不必,只是没有php变量时,没有办法进行动态输入),一个Mysql
变量。

热点内容
加密狗是啥 发布:2025-02-06 03:48:03 浏览:552
phpcopy文件 发布:2025-02-06 03:41:26 浏览:410
系统配置页面怎么设置 发布:2025-02-06 03:36:34 浏览:693
家庭电脑搭建流媒体服务器 发布:2025-02-06 03:19:02 浏览:342
matlab稀疏矩阵存储 发布:2025-02-06 03:07:54 浏览:838
国际服2b2t服务器地址 发布:2025-02-06 03:06:28 浏览:390
c语言输出b 发布:2025-02-06 03:06:27 浏览:31
普通火车wifi密码多少 发布:2025-02-06 03:04:20 浏览:436
可编程监控 发布:2025-02-06 03:03:33 浏览:645
c语言取随机数 发布:2025-02-06 02:46:57 浏览:863