mysql中的存储过程

存储过程的作用:有助于提高应用程序的性能。存储过程可以不必发送多个冗长的SQL语句

废话不说多,直接实操

##实现num的相加
delimiter $$
CREATE PROCEDURE test1 ()
begin
	
	declare num int default 0;		-- 声明变量,赋默认值为0
	select num+20;
	
end $$
delimiter ;                 --将结束符修改成;

call test1();			-- 调用存储过程
drop procedure test1   --如果不需要此存储函数开源删除咯
set赋值操作
delimiter $$

CREATE PROCEDURE test2 ()
begin
	
	declare num int default 0;
	set num =50;			-- 给num变量赋值
	select num;
	
end $$
delimiter ;

call test2();
into的使用方法
delimiter $$
CREATE PROCEDURE test3 ()
begin
	
	declare num int default 0;			
	select count(1) into num from t_student_info;  --计算t_student_info表的个数用num来记录
	select num;
end $$
delimiter ;
drop procedure test3;   删除该存储函数
call test3();
if的使用
delimiter $$
CREATE PROCEDURE test4 ()
begin
	
	declare id int default 1;			
	declare class_name varchar(20);
	
	if id=1 then
		set class_name='要多久你才可以爱上我!';
	elseif id=2 then
		set class_name='不再让自己遗憾了';
	else
		set class_name='不用想了,谁都不爱我,我只爱我自己';
	end if;
	
	select class_name;
end $$
delimiter ;

call test4();


mysql> delimiter;
ERROR: 
DELIMITER must be followed by a 'delimiter' character or string

使用delimiter;会报错,一定要带空格
 

 定义一个输入参数
delimiter $$
CREATE PROCEDURE test5 (in id int)
begin
	
	declare class_name varchar(20);
	
	if id=1 then
		set class_name='我和xhell脚本的if不一样!';
	elseif id=2 then
		set class_name='我和python中的if语法有一点带你不一样';
	else
		set class_name='不用想了,不靠别人';
	end if;
	select class_name;  
注:存储过程中声明了 class_name 变量并对其进行了赋值,但并没有通过 SELECT 语句来显示其值。你需要在存储过程末尾添加 SELECT class_name; 语句,以便在调用存储过程时返回 class_name 的值
	
end $$
delimiter ;

call test5(3);
case的使用
delimiter $$
CREATE PROCEDURE test6 (in month int,out season varchar(10))
begin
	
	case 
		when month >=1 and month<=3 then
			set season='spring';
		when month >=4 and month<=6 then
			set season='summer';
		when month >=7 and month<=9 then
			set season='autumn';
		when month >=10 and month<=12 then
			set season='winter';
	end case;
end $$
delimiter ;

call test6(9,@season);			-- 定义会话变量来接收test8存储过程返回的值

select @season;
@xxx:代表定义一个会话变量,整个会话都可以使用,当会话关闭(连接断开)时销毁
@@xxx:代表定义一个系统变量,永久生效。

while循环的使用
delimiter $$
CREATE PROCEDURE test7 (in count int)
begin
	declare total int default 0;
	declare i int default 1;
	
	while i<=count do
		set total=total+i;
		set i=i+1;
	end while;
	select total;
end $$
delimiter ;

call test7(10);

repeat的使用
delimiter $$
CREATE PROCEDURE test7 (count int)		  -- 默认是输入(in)参数
begin
	declare total int default 0;
	repeat 
		set total=total+count;
		set count=count-1;
		until count=0				-- 结束条件,注意不要打分号
	end repeat;
	select total;
end $$
delimiter ;

call test8(10);


使用 select total; 语句输出 total 的最终值。
total 是局部变量,只在该存储过程内部有效。
而 @total 是用户变量,可以在整个会话中使用和共享。

loop的使用
delimiter $$
CREATE PROCEDURE test9 (count int)		 -- 默认是输入(in)参数
begin
	declare total int default 0;	
	sum:loop				-- 定义循环标识		 	
		set total=total+count;
		set count=count-1;
		
		if count < 1 then
			leave sum;	 -- 跳出循环			
		end if;
	end loop sum;			 -- 标识循环结束		
	select total;
	
end $$
delimiter ;

call test9(10);
创建一张临时表:
create temporary table temp_table(
	id int,
	name varchar(10)
);
insert into temp_table values (1,'xiaoxiaowang');

select * from temp_table ;
注意:临时表示查询不到的
show tables;   -- 不会显示临时表的存在
测试存储过程创建临时表是可以查到的,但是在存储函数中是查看不到的,结果会报错:
create procedure pro1()
begin
	create temporary table temp_table(
		id int
	);
	
	insert into temp_table values(1);
	
	select * from temp_table;
end;

call pro1();

测试存储函数创建临时表
create function fun2()
returns int
begin

	declare id int ;
	create table temp_table(				
		id int
	);
	
	insert into temp_table values(1);
	
	select id from into id temp_table;	
	return id;
end;

怎么查看自己创建了多少的存储过程????????????

SHOW PROCEDURE STATUS WHERE Db = 'your_database_name';
咱们的业务应该放到咱们的业务层,而不是把业务滞留到数据库来处理,将业务和数据库严重耦合在一起了!这是导致公司开发不使用存储过程的


 

相关推荐

  1. MySQL存储过程

    2024-07-13 20:12:02       39 阅读
  2. mysql触发器,存储过程

    2024-07-13 20:12:02       39 阅读
  3. Mysql 存储过程(procedure)

    2024-07-13 20:12:02       27 阅读
  4. MySQL视图、存储过程、触发器

    2024-07-13 20:12:02       32 阅读
  5. mysql_存储过程

    2024-07-13 20:12:02       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 20:12:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 20:12:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 20:12:02       57 阅读
  4. Python语言-面向对象

    2024-07-13 20:12:02       68 阅读

热门阅读

  1. C++多态

    C++多态

    2024-07-13 20:12:02      19 阅读
  2. html自学笔记与面试会问到问题

    2024-07-13 20:12:02       19 阅读
  3. 【Go系列】 Go的错误处理

    2024-07-13 20:12:02       21 阅读
  4. 【学习笔记】Redis学习笔记——第13章 客户端

    2024-07-13 20:12:02       20 阅读
  5. 给基于cmake的工程添加uninstall功能

    2024-07-13 20:12:02       17 阅读
  6. js登陆验证

    2024-07-13 20:12:02       17 阅读
  7. Linux学习笔记(二)

    2024-07-13 20:12:02       19 阅读
  8. 2024 暑假友谊赛 1

    2024-07-13 20:12:02       22 阅读
  9. python合并列表的方法

    2024-07-13 20:12:02       23 阅读
  10. 中药学--更新中

    2024-07-13 20:12:02       16 阅读