mysql中的存储过程和游标

Posted 哈根达斯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql中的存储过程和游标相关的知识,希望对你有一定的参考价值。

 
	-- mysql 存储过程
/*
	set @result = 0;
	create procedure login(		-- 定义参数,有in、out、inout三种类型
		in user varchar(10),
		in pass varchar(10),
		out result int
	)
	begin 
		declare passd varchar(10);-- declare 声明临时变量、类型,然后用set 进行赋值,declare 临时变量只能放在begin end 区域中,而其作用域也只是在begin end 中, 而 set @ 定义的变量是全局变量
		select password into passd from login where username=user;
		if passd like pass then	--  If 语句,后面要加上 End IF,就像是case 后也要加 End Case  一样
		select ‘Login Success‘ as Massage;
		set result = 1;
		else
		select ‘Login Failed‘ as Message;
		set result =0;
		end if;
	end;
*/
 
-- 调用存储过程  call login(‘root‘,‘root‘,@result);
-- 删除存储过程  drop procedure login
 
create procedure translate(
	id int
)
begin
	case id
	when 1 then   
	select ‘one‘ as trans;
	when 2 then
	select ‘two‘ as trans;
	when 3 then 
	select ‘three‘ as trans;
	else
	select ‘no trans‘ as trans;
	end case;
end;
 
/*
	case 用法有两种:
	1. 条件变量在when 中
		select name, case
			when age>10 then xxxxx
			else
				xxxxxx
			end case
	2. 条件变量在case 中
		select name,case age
		when >10 then xxx 
		else xxxxxs
		end case
*/

 

以上是关于mysql中的存储过程和游标的主要内容,如果未能解决你的问题,请参考以下文章

mysql中的存储过程和游标

MySQL存储过程和游标

MySQL 存储过程,获取使用游标查询的结果集

mysql存储过程 使用游标实现两张表数据同步数据

带有游标的 MySQL 存储过程

MySQL游标存储过程条件显示重复记录