美文网首页
Mysql存储过程Demo

Mysql存储过程Demo

作者: 子喻爱吃黄焖鸡 | 来源:发表于2017-07-14 17:20 被阅读0次

基础结构

DROP PROCEDURE IF EXISTS temp;
DELIMITER //
CREATE PROCEDURE temp()
BEGIN
  DECLARE a INT DEFAULT 1;
 
  SET a=a+1;
  SET @b=@b+1;
  SELECT a,@b;
 
END
//
DELIMITER ;

mysql执行动态sql

delimiter //  
create procedure proce2(in old varchar(100), in newT varchar(100))  
begin  
        declare my_sql varchar(500);  
        set my_sql = concat('create table ',newT,' like ',old);  
        set @ms = my_sql;  
        prepare s1 from @ms;  
        execute s1;  
        deallocate prepare s1;  
end
// 
DELIMITER ; 

存储过程的事务控制(提交和回滚)以及异常处理

create table hppluginsetting(
pid varchar(50),
plugindesc varchar(1000),
filepath varchar(1000),
isuse int,
ordernum int
)
;
drop procedure if exists p_addplugintoportal; 
DELIMITER //
create  procedure p_addplugintoportal(newid varchar(4000),plugindesc varchar(4000),filepath varchar(4000),existid varchar(4000))
begin

DECLARE temp_order int DEFAULT 0;
DECLARE strsql varchar(4000);
DECLARE t_error INTEGER DEFAULT 0;  
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;  

select  max(ordernum) into temp_order from hppluginsetting where pid = existid;

IF temp_order is null or temp_order ='' THEN 
   begin
   select max(ordernum) into temp_order from hppluginsetting;
   end;
select "123"+temp_order;
ELSEIF temp_order is not null THEN 
-- 事务控制
 START TRANSACTION; 
  begin  
        set strsql:= concat('UPDATE hppluginsetting set ordernum = ordernum +1 where ordernum >',temp_order);  
        set @strsql = strsql;  
        prepare s1 from @strsql;  
        execute s1;  
        deallocate prepare s1;  
select strsql;
  end; 
ELSE 
  set temp_order:=0;
END IF;
begin 
insert into hppluginsetting(pid,plugindesc,filepath,isuse,ordernum) values (newid,plugindesc,filepath,1,temp_order+1);
end;
 IF t_error = 1 THEN  
    ROLLBACK;  
 ELSE  
    COMMIT;  
 END IF; 
end
//
DELIMITER ;

call p_addplugintoportal ('checkPlugin','插件下载检查','/js/init_wev8.js,/js/activex/ActiveX_wev8.js,/wui/common/js/plugin/checkPlugin/checkPlugin.js','0') 

疑问:

以下用两个prepare预处理第二条insert并没有执行

drop procedure if exists p_addplugintoportal; 
DELIMITER //
create  procedure p_addplugintoportal(newid varchar(4000),plugindesc varchar(4000),filepath varchar(4000),existid varchar(4000))
begin

DECLARE temp_order int DEFAULT 0;
DECLARE strsql varchar(4000);
DECLARE t_error INTEGER DEFAULT 0;  
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;  

select  max(ordernum) into temp_order from hppluginsetting where pid = existid;

IF temp_order is null or temp_order ='' THEN 
   begin
   select max(ordernum) into temp_order from hppluginsetting;
   end;
ELSEIF temp_order is not null THEN 
-- 事务控制
 START TRANSACTION; 
  begin  
        set strsql:= concat('UPDATE hppluginsetting set ordernum = ordernum +1 where ordernum >',temp_order);  
        set @strsql = strsql;  
        prepare s1 from @strsql;  
        execute s1;  
        deallocate prepare s1;  
  end; 
ELSE 
  set temp_order:=0;
END IF;
  set strsql:=concat('insert into hppluginsetting(pid,plugindesc,filepath,isuse,ordernum) values (',newid,',',plugindesc,',',filepath,',',1,',',temp_order+1,')');
   set @strsql2 = strsql;  
        prepare s2 from @strsql2;  
        execute s2;  
        deallocate prepare s2;
 IF t_error = 1 THEN  
    ROLLBACK;  
 ELSE  
    COMMIT;  
 END IF; 
end
//
DELIMITER ;

相关文章

  • Mysql存储过程Demo

    基础结构 mysql执行动态sql 存储过程的事务控制(提交和回滚)以及异常处理 疑问: 以下用两个prepare...

  • 15 MySQL 存储过程

    MySQL 存储过程 [toc] 存储过程概述 存储过程介绍 存储过程,相当于是 MySQL 语句组成的脚本 指的...

  • 存储过程

    使用存储过程 1.存储过程 ​ 需要MySQL5---->> MySQL5添加了对存储过程的支持。 ​ 迄...

  • MySQL存储过程详解 mysql 存储过程

    原文链接 MySQL存储过程详解 1.存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然...

  • Mysql存储过程

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 创建存储过程 调用存储过程 存储过程...

  • 面试官突然问我MySQL存储过程,我竟然连基础都不会!(详细)

    MySQL存储过程 一、存储过程 1.1 什么是存储过程 存储过程(Stored Procedure)是在大型数据...

  • MySQL存储过程创建及调用方法

    MySQL存储过程是一个sql语句,那么我们如何创建呢,MySQL存储过程创建及修改,删除操作。 1,存储过程创建...

  • 存储过程

    创建存储过程 mysql: 执行存储过程 mysql: 使用OUT参数和INOUT参数: PREPARE和EXEC...

  • SQL基本语句(以MySQL为例)-- 第五部分

    19、使用存储过程MySQL的存储过程的语句跟这本书上的差太远了吧,以下参考自:MySQL存储过程教程 20、管理...

  • MySQL存储过程和存储函数

    一、存储过程 MySQL存储过程存储过程和存储函数参考文章 SQL语句需要先编译然后执行,而存储过程(Stored...

网友评论

      本文标题:Mysql存储过程Demo

      本文链接:https://www.haomeiwen.com/subject/xckohxtx.html