Home > Database > Mysql Tutorial > mysql使用游标将select结果插入其他表中

mysql使用游标将select结果插入其他表中

WBOY
Release: 2016-06-07 14:57:29
Original
1225 people have browsed it

mysql使用游标将select结果插入其他表中 无 BEGIN#定义 变量DECLARE a VARCHAR(32);DECLARE b VARCHAR(200);DECLARE c text ;DECLARE d VARCHAR(32);DECLARE e VARCHAR(100);DECLARE f int(11);DECLARE g VARCHAR(100);DECLARE h VARCHAR(100);#此变可有可无

mysql使用游标将select结果插入其他表中
BEGIN
#定义 变量
DECLARE a VARCHAR(32);
DECLARE b VARCHAR(200);
DECLARE c text ;
DECLARE d VARCHAR(32);
DECLARE e VARCHAR(100);
DECLARE f int(11);
DECLARE g VARCHAR(100);
DECLARE h VARCHAR(100);

#此变可有可无,为了给个该存储函数执行成功后给个提示,运行下便知道
DECLARE count_num int(11);
DECLARE x int;

#这个用于处理游标到达最后一行的情况
DECLARE s int default 0;

#声明游标cursor_name(cursor_name是个多行结果集)
DECLARE cursor_name CURSOR FOR select NNF_ID,NNF_NAME,NNF_REALNAME,NNF_NOTICE_ID from t_notice_file;

#设置一个终止标记 
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
set count_num = 0;
				
#打开游标
OPEN cursor_name;

#获取游标当前指针的记录,读取一行数据并传给变量a,b
fetch  cursor_name into a,b,c,d;
					
#开始循环,判断是否游标已经到达了最后作为循环条件 
while s <> 1 do
set count_num =  count_num+1;
insert into t_ehdnotice_file(id,name,original_name,create_time,ehdnotice_id,path) values(a,b,c,now(),d,'2014-04\\15');
							
#读取下一行的数据
fetch  cursor_name into a,b,c,d;
end while;
				 
#关闭游标
CLOSE cursor_name ;
select count_num as"新增条数";

#语句执行结束
END
Copy after login
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template