search
HomeDatabaseMysql TutorialMySQL存储过程使用实例详解_MySQL

bitsCN.com

例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 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  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       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
('procedure1', 'pass1'),
('procedure2', 'pass2'),
('procedure3', 'pass3'),
('procedure4', 'pass4');下面就是一个简单存储过程的例子:
drop procedure IF EXISTS test_proc;
delimiter //
create procedure 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 procedure IF EXISTS test_proc_1;
delimiter //
create procedure 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 procedure IF EXISTS test_proc_2;
delimiter //
create procedure 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();

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),