【MYSQL】存储过程示例
GROUPEMP_EXISTS: CREATE DEFINER=`ucheck`@`%` PROCEDURE `GROUPEMP_EXISTS`(IN `projectype` varchar(50), IN `groupid` int(11), IN `empid` int(11))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE projectid VARCHAR(10) DEFAULT NULL;
DECLARE result VARCHAR(200) DEFAULT '';
DECLARE project CURSOR FOR SELECT project_id FROM u_project WHERE u_project.project_type=projectype;
DECLARE CONTINUE HANDLER FOR SQLSTATE '' SET projectid=NULL;
OPEN project;
search_Loop:LOOP
FETCH project INTO projectid;
IF projectid IS NOT NULL THEN
SET @num = 0;
SET @sqlstr = CONCAT("SELECT COUNT(*) into @num FROM project_",projectid,".u_project_group_emp WHERE group_id=",groupid," AND emp_id=",empid);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
IF @num!=0 THEN
IF result=''
THEN
SET result =projectid;
ELSE
SET result=CONCAT(result,',',projectid);
END IF;
END IF;
ELSE
LEAVE search_loop;
END IF;
END LOOP search_loop;
CLOSE project;
SELECT result;
END
GROUP_EXISTS: CREATE DEFINER=`ucheck`@`%` PROCEDURE `GROUP_EXISTS`(IN `projectype` varchar(50), IN `groupid` int(11))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT '根据指定的组id,查询所有库,返回存在该组id的项目id (1,2,3,4)'
BEGIN
DECLARE projectid VARCHAR(10) DEFAULT NULL;
DECLARE result VARCHAR(200) DEFAULT ''; DECLARE project CURSOR FOR SELECT project_id FROM u_project WHERE u_project.project_type=projectype;
DECLARE CONTINUE HANDLER FOR SQLSTATE '' SET projectid=NULL;
OPEN project;
REPEAT
FETCH project INTO projectid;
IF projectid IS NOT NULL
THEN
SET @num = 0;
SET @sqlstr = CONCAT("SELECT COUNT(*) into @num FROM project_",projectid,".u_project_group_emp WHERE group_id=",groupid);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
IF @num!=0 THEN
IF result=''
THEN
SET result =projectid;
ELSE
SET result=CONCAT(result,',',projectid);
END IF;
END IF;
END IF;
UNTIL projectid IS NULL END REPEAT;
CLOSE project;
SELECT result;
END
UPDATE_REVIEWOK CREATE DEFINER=`ucheck`@`%` PROCEDURE `UPDATE_REVIEWOK`(IN `projectId` int, IN `shotId` int, OUT `result` int)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN DECLARE EXIT HANDLER FOR SQLEXCEPTION SET result=-1;
SET result=1; SET @reel='';
SET @shot=0;
set @sql=concat("SELECT shot_lock,shot_reel INTO @shot,@reel FROM project_",projectId,".u_shot WHERE shot_id=",shotId," FOR UPDATE");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt; IF @shot=1 THEN
set @sql=concat("UPDATE project_",projectId,".u_mastershotlist_part,project_",projectId,".u_mastershotlist SET dmap_review_ok=NULL WHERE u_mastershotlist.shot_id=",shotId," AND u_mastershotlist.id=u_mastershotlist_part.mastershotlist_id");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
ELSE
SET @picstatus=0;
set @sql=concat("SELECT status_id INTO @picstatus FROM uPlatform.u_status WHERE status_name='Review OK' AND status_type=0");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
SET @reviewoknum=0;
SET @totalnum=0;
set @sql=concat("SELECT SUM(IF(picture_status=",@picstatus,",1,0))AS reviewok,COUNT(*)AS totalNum INTO @reviewoknum,@totalnum FROM reel_",projectId,"_",@reel,".u_picture WHERE picture_shot=",shotId," AND picture_lock=0");
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt;
IF @totalNum=0 THEN
SET @per=NULL;
set @sql=concat("UPDATE project_",projectId,".u_mastershotlist_part,project_",projectId,".u_mastershotlist SET dmap_review_ok=NULL WHERE u_mastershotlist.shot_id=",shotId," AND u_mastershotlist.id=u_mastershotlist_part.mastershotlist_id");
ELSE
SET @per=@reviewoknum /@totalnum;
set @sql=concat("UPDATE project_",projectId,".u_mastershotlist_part,project_",projectId,".u_mastershotlist SET dmap_review_ok=",@per," WHERE u_mastershotlist.shot_id=",shotId," AND u_mastershotlist.id=u_mastershotlist_part.mastershotlist_id");
END IF;
prepare stmt from @sql;
execute stmt;
deallocate prepare stmt; END IF; END
mysql> PREPARE prod FROM "INSERT INTO examlple VALUES(?,?)";
mysql> SET @p='';
mysql> SET @q='';
mysql> EXECUTE prod USING @p,@q;
mysql> SET @name='';
mysql> EXECUTE prod USING @p,@name;
mysql> DEALLOCATE PREPARE prod; #EXECUTE stmt USING @a;这样的语句USING后面的变量也只能用set @var=value这种,declare和参数传入的变量不行。
使用 PREPARE 的几个注意点:
A:PREPARE stmt_name FROM preparable_stmt;预定义一个语句,并将它赋给 stmt_name ,tmt_name 是不区分大小写的。
B: 即使 preparable_stmt 语句中的 ? 所代表的是一个字符串,你也不需要将 ? 用引号包含起来。
C: 如果新的 PREPARE 语句使用了一个已存在的 stmt_name ,那么原有的将被立即释放! 即使这个新的 PREPARE 语句因为错误而不能被正确执行。
D: PREPARE stmt_name 的作用域是当前客户端连接会话可见。
E: 要释放一个预定义语句的资源,可以使用 DEALLOCATE PREPARE 句法。
F: EXECUTE stmt_name 句法中,如果 stmt_name 不存在,将会引发一个错误。
G: 如果在终止客户端连接会话时,没有显式地调用 DEALLOCATE PREPARE 句法释放资源,服务器端会自己动释放它。
H: 在预定义语句中,CREATE TABLE, DELETE, DO, INSERT, REPLACE, SELECT, SET, UPDATE, 和大部分的 SHOW 句法被支持。
I: PREPARE 语句不可以用于存储过程,自定义函数!但从 MySQL 5.0.13 开始,它可以被用于存储过程,仍不支持在函数中使用!
【MYSQL】存储过程示例的更多相关文章
- 创建MySQL存储过程示例
创建MySQL存储过程是学习MySQL数据库必须要掌握的知识,下文对创建MySQL存储过程作了详细的介绍,供您参考学习. AD:2013大数据全球技术峰会课程PPT下载 下文将教您如何创建MySQL存 ...
- MySQL存储过程示例
MySQL存储过程: /*自定义结束符*/ DELIMITER $$ /*如果存在同名的存储过程就删除*/ DROP PROCEDURE IF EXISTS prAddBlack$$ /*创建存储过程 ...
- mysql 存储过程 php版本
<?php /** * PHP操作Mysql存储过程示例 * * @author flyer0126 * @date 2011-12-23 * */ //配置数据库连接信息 $hostname ...
- MySQL存储过程(更新指定字段的数据)
mysql存储过程示例: USE 数据库名称;DROP PROCEDURE IF EXISTS 数据库名称.存储过程名称;delimiter $$CREATE PROCEDURE 数据库名称.存储过程 ...
- MySQL 存储过程传参数实现where id in(1,2,3,...)示例
一个MySQL 存储过程传参数的问题想实现例如筛选条件为:where id in(1,2,3,...),下面有个不错的示例,感兴趣的朋友可以参考下 正常写法: ,,,,...); 当在写存储过程 ...
- mysql存储过程和存储函数
mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...
- Mysql存储过程调用
mysql存储过程实例教程 发布时间:2014-04-09编辑:JB01 这篇文章主要介绍了mysql存储过程的使用方法,mysql存储过程实例教程,有需要的朋友参考下. 1.1create p ...
- Mysql存储过程查询结果赋值到变量的方法
Mysql存储过程查询结果赋值到变量的方法 把查询结果赋值到变量,大部分情况下使用游标来完成,但是如果明确知道查询结果只有一行(例如统计记录的数量,某个字段求和等),其实可以使用set或into的 ...
- mysql存储过程之异常处理篇
mysql存储过程也提供了对异常处理的功能:通过定义HANDLER来完成异常声明的实现 语法如下: DECLARE handler_type HANDLER FOR condition_value[, ...
- JDBC使用MySQL存储过程错误
JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedur ...
随机推荐
- Echarts如何添加鼠标点击事件?防止重复触发点击事件
Echarts如何添加鼠标点击事件? 1.通常我们只使用了以下代码,通过配置项和数据显示图表. var myChart = echarts.init(document.getElementById(' ...
- 【异常】ser class threw exception: java.sql.SQLException: The last packet successfully received from the server was 39,444 milliseconds ago. The last
1 详细异常 ser class threw exception: java.sql.SQLException: The last packet successfully received from ...
- Codeforces 567C - Geometric Progression - [map维护]
题目链接:https://codeforces.com/problemset/problem/567/C 题意: 给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中, ...
- Lua 学习之基础篇八<Lua 元表(Metatabble)&&继承>
讲到元表,先看一段table的合并动作. t1 = {1,2} t2 = {3,4} t3 = t1 + t2 attempt to perform arithmetic on a table val ...
- Windows Dialog对话框
一.MessageBox弹出框 MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon ...
- jaxb读有BOM的XML文件问题
一开始找了半天没找到什么原因,读文件就报错: Content is not allowed in prolog 后来发现是文件是UTF-8带BOM和不带BOM的问题 问题就好解决了,直接读带BOM文件 ...
- django获取数据
获取单个值 request.POST.get('user') # user对应前端name属性对应的值 获取多个值(如checkbox,multiple) request.POST.getlist(' ...
- Codeforces Round #588 (Div. 2) D题【补题ING】
思路:先找出现次数>=2数.然后在取跑所有数,需要考虑一般情况(当一个人比另一个人的ai小且他们的与运算等于小的那个人的ai那么可以知道大的那个人必定强于ai小的那个人). 则可以用位运算实现判 ...
- PHP mysqli_connect_errno() 函数
返回上一次连接错误的错误代码: <?php $con=mysqli_connect("localhost","wrong_user","my_p ...
- Oracle 11g关闭用户连接审计
sys.aud$表数据量增长较快.这时,不想关闭数据库的审计,但是又不想频繁的清理sys.aud$表.可进行如下操作关闭数据库连接的审计 noaudit connect;