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

mysql存储过程ifnot

发布时间: 2023-06-26 12:34:57

❶ mysql存储过程中先判断数据库中是否存在table1表,有就删除,没有就新建

是的 这个主要是在增删改查的时候用到
if TABLE1 EXISTING DROP TABLE1;
CREATE TABLE

❷ 五、MYSQL存储过程和函数

• create procere用来创建 存储过程 ,create function用来创建 函数

Delimiter命令是改变语句的结束符 ,MySQL默认的结束符为;号,由于procere和function中的;号并不代表创建的结束,所以要替换成另外的结束符以便表示创建的结束
• rontine_body子句可以包含一个简单的SQL语句,也可以包含多个SQL语句, 通过begin…end将这多个SQL语句 包含在一起
• MySQL存储过程和函数中也可以包含类似create和drop等DDL语句
• comment子句用来写入对存储过程和函数的注释
Language子句用来表示此存储过程和函数的创建语言
存储过程和函数被标注为deterministic表明当输入相同的参数是会返回相同的结果,反之如果是not deterministic则表示相同参数不会是相同结果,默认是not deterministic

相关属性短语只有咨询含义,并不是强制性的约束

• Drop procere/function语句用来 删除指定名称的存储过程或函数

• Begin…end语句通常出现在存储过程、函数和触发器中,其中 可以包含一个或多个语句 ,每个语句用;号隔开

• 标签label可以加在begin…end语句以及loop, repeat和while语句
语句中通过iterate和leave来控制流程,iterate表示返回指定标签位置,leave表示跳出标签

Declare语句通常用来声明本地变量、游标、条件或者handler
Declare语句只允许出现在begin … end语句中而且必须出现在第一行
Declare的顺序也有要求,通常是先声明本地变量,再是游标,然后是条件和handler

• 本地变量可以通过declare语句进行声明
声明后的变量可以通过select … into var_list进行赋值,或者通过set语句赋值,或者通过定义游标并使用fetch … into var_list赋值
• 通过declare声明变量方法:

• MySQL支持if,case,iterate,leave,loop,while,repeat语句作为存储过程和函数中的 流程控制语句 ,另外return语句也是函数中的特定流程控制语句

• Case语句在存储过程或函数中表明了 复杂的条件选择语句

• IF语句在存储过程或函数中表明了 基础的条件选择语句

其中在 function 里面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我们开启了 bin-log, 我们就必须为我们的 function 指定一个参数。
在 MySQL 中创建函数时出现这种错误的解决方法:
set global log_bin_trust_function_creators=TRUE;

• Iterate语句 仅出现在loop,repeat,while循环语句中,其含义表示重新开始此循环

• Leave语句表明 退出指定标签的流程控制语句块
• 通常会用在begin…end,以及loop,repeat,while的循环语句中

• Loop语句是存储过程或函数中表达 循环执行 的一种方式

• repeat语句是存储过程或函数中表达 循环执行 的一种方式

• while语句是存储过程或函数中表达 循环执行 的一种方式

• Return语句用在 函数中,用来终结函数的执行并将指定值返回给调用者

• Cursor游标用来 声明一个数据集
• 游标的声明必须在变量和条件声明之后,在handler声明之前

• Cursor close语句用来 关闭之前打开的游标

• Cursor declare语句用来声明一个游标和指定游标对应的数据集合, 通常数据集合是一个select语句

• Cursor fetch语句用来获取游标指定数据集的 下一行数据 并将各个字段值赋予后面的变量

• Open cursor语句用来打开一个之前已经 声明好的游标

• Declare condition语句命名 特定的错误条件 ,而该特定错误可以在declare…handler中指定 处理方法

• 比如在MySQL中1051error code表示的是unknown table的错误,如果要对这
个错误做特殊处理,可以用三种方法:

• Declare handler语句用来声明一个handler来处理一个或多个特殊条件,当其中的某个条件满足时则触发其中的statement语句执行
• Statement可以是一个简单SQL语句,也可以是begin…end组成的多个语句

• Handler_action子句声明当执行完statement语句之后应该怎么办

Condition_value的值有以下几种:

• 当condition发生但没有声明handler时,则存储过程和函数依照如下规则处理

• create trigger语句用来创建一个触发器,触发器的作用是当表上有对应SQL语句发生时,则触发执行
• 触发器创建时需要 指定对应的表名 tbl_name

Definer关键词用来指定trigger的安全环境
• Trigger_time指定触发器的执行时间,BEFORE和AFTER指定触发器在表中的 每行数据修改前或者后 执行
• Trigger_event指定触发该触发器的具体 事件
• INSERT当新的一行数据插入表中时触发,比如通过执行insert,load data,replace语句插入新数据
• UPDATE当表的一行数据被修改时触发,比如执行update语句时
• DELETE当表的一行数据被删除时触发,比如执行delete,replace语句时
• 当执行insert into … on plicate key update语句时,当碰到重复行执行update时,则触发update下的触发器
• 从5.7.2版本开始,可以创建具有相同trigger_time和trigger_event的同一个表上的多个触发器,默认情况下按照创建的时间依次执行,通过 指定FOLLOWS/PRECEDES改变执行顺序 ,即FOLLOWS时表示新创建的触发器后执行,PRECEDES则表示新触发器先执行
• Trigger_body表示触发器触发之后要执行的一个或多个语句,在内部可以引用涉及表的字段, OLD.col_name表示行数据被修改或删除之前的字段数据,NEW.col_name表示行数据被插入或修改之后的字段数据

• Drop trigger语句用来 删除一个触发器

• If exists短语用来避免删除不存在的触发器时引发报错
当你执行drop table时,表上的触发器也被drop掉了

❸ mysql存储过程的if判断有多个条件该怎么优化效率

这个应该不会太慢吧,我建议你看一下,你是不是循环做了太多次的插入/更新操作。
mysql默认的配置中,每次事务提交都要写binlog和redo log,如果循环太多次——比如循环插入10w条记录——就会非常慢。一般优化思路分两种:
1 修改 sync_binlog为一个100-1000间的值,让binlog每隔100-1000个事务后再写一次;修改innodb_flush_log_at_trx_commit =2; 这么搞的好处是降低了写log的次数和消耗的时间,缺点是,中间出错的话,会丢失一部分的binlog和redolog导致无法通过他们来在出问题是恢复生产库数据。
2 将所有的插入/更新操作放到一个事务中进行。这样,显然就只需要一次写binlong和redolog咯。

❹ 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 存储过程

你应该在做统计吧,估计你不会的就是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 存储过程总结(二)if语句、参数

1、if :用于做条件判断,具体的语法结构为:

在if条件判断的结构中,ELSE IF 结构可以有多个,也可以没有。 ELSE结构可以有,也可以没有。

案列:

根据定义的分数score变量,判定当前分数对应的分数等级。

score >= 90分,等级为优秀。

score >= 80分,等级为良好

score >= 60分,等级为及格

score < 60分,等级为不及格。

上述的需求我们虽然已经实现了,但是也存在一些问题,比如:score 分数我们是在存储过程中定义 死的,而且最终计算出来的分数等级,我们也仅仅是最终查询展示出来而已。

那么我们能不能,把score分数动态的传递进来,计算出来的分数等级是否可以作为返回值返回呢? 答案是肯定的,我们可以通过接下来所讲解的 参数 来解决上述的问题。

2、参数的类型

主要分为以下三种:IN、OUT、INOUT。 具体的含义如下:

(1)in :该类参数作为输入,也就是需要调用时传入值 默认

(2)out:该类参数作为输出,也就是该参数可以作为返回值

(3)inout:既可以作为输入参数,也可以作为输出参数

用法:

案例一:

案列二:

❼ mysql存储过程中,loop……end loop后用select 列明,无法显示数据,结果显示no data to fetch,为什么

要做异常捕捉
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

实例如下:
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;
OPEN cur2;

REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;

CLOSE cur1;
CLOSE cur2;
END

❽ navicat 8.0 mysql中如何调用存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `curdemo`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE userid,repid INT;
DECLARE cur1 CURSOR FOR select u.userid,u.repid from user u,personalwebsite p where
p.url u.repid and p.distributorid = u.userid
and p.url = u.userid;
DECLARE CONTINUE HANDLER FOR SQLSTATE \'02000\' SET done = 1;
OPEN cur1;
REPEAT
FETCH cur1 INTO userid,repid;
IF NOT done THEN
update personalwebsite set url = repid where distributorid = userid;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
END;

热点内容
安卓哪个国家免费用 发布:2025-02-04 22:39:54 浏览:59
电脑配置低但想玩小偷模拟器怎么办 发布:2025-02-04 22:39:03 浏览:233
最快脚本语言 发布:2025-02-04 22:27:23 浏览:527
安卓的人脸识别在哪里 发布:2025-02-04 22:16:45 浏览:674
悠然服务器的ip是什么 发布:2025-02-04 22:10:17 浏览:65
3des源码 发布:2025-02-04 22:09:16 浏览:809
如何备份数据库表 发布:2025-02-04 22:09:07 浏览:294
如何删除下载的闹钟铃声安卓 发布:2025-02-04 22:03:35 浏览:659
死神脚本 发布:2025-02-04 21:57:03 浏览:168
phpposthtml 发布:2025-02-04 21:37:46 浏览:89