---
title: "mysql数据游标嵌套写法问题"
author: "雪狐狼"
date: "2013-06-26T15:09:23Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2013-06-26T15:09:23Z"
id: 1894711
url: "https://redvesper.iteye.com/blog/1894711"
---

如下代码来自  [http://www.iteye.com/topic/1026084](http://www.iteye.com/topic/1026084) 

1.drop procedure if exists \`sp\_nested\_cursor\`;    
2.create procedure \`sp\_nested\_cursor\`()    
3.begin   
4.    declare v\_uid bigint(22);    
5.    declare v\_code varchar(100);    
6.    declare v\_value varchar(100);    
7.    declare \_done TINYINT(1) default 0;    
8.    declare cur\_user cursor for select id from \`tb\_user\`;    
9.    declare continue handler for not found set \_done = 1;    
10.        
11.    open cur\_user;    
12.    loop\_xxx:loop    
13.        fetch cur\_user into v\_uid;    
14.        if \_done=1 then   
15.            leave loop\_xxx;    
16.        end if;    
17.        begin   
18.            declare \_inner tinyint(1) default 0;    
19.            declare cur\_param cursor for select code, value     
20.                                         from \`tb\_user\_param\`     
21.                                         where user\_id=v\_uid;    
22.            declare continue handler for not found set \_inner = 1;     
23.            open cur\_param;    
24.            loop\_yyy:loop    
25.                fetch cur\_param into v\_code, v\_value;    
26.                if \_inner=1 then   
27.                    leave loop\_yyy;    
28.                end if;    
29.                insert into tb\_key\_value values (v\_uid, v\_code, v\_value);    
30.            end loop;    
31.            commit;    
32.        end;    
33.    end loop;    
34.end;  

 

 

在sqlyog enterprise工具中执行

Error Code : 1064  
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'loop\_yyy:loop  
                 fetch cur\_param into v\_code, v\_value' at line 1  
(0 ms taken)

mysql5.0 不支持这种写法？