保定哪家做网站好,平面设计师工资一般多少钱一个月,虚拟主机技术,网站建设基本流程包括哪几个步骤存储过程的作用:有助于提高应用程序的性能。存储过程可以不必发送多个冗长的SQL语句
废话不说多#xff0c;直接实操
##实现num的相加
delimiter $$
CREATE PROCEDURE test1 ()
begindeclare num int default 0; -- 声明变量,赋默认值为0select num20;end $$
delimiter ; …存储过程的作用:有助于提高应用程序的性能。存储过程可以不必发送多个冗长的SQL语句
废话不说多直接实操
##实现num的相加
delimiter $$
CREATE PROCEDURE test1 ()
begindeclare num int default 0; -- 声明变量,赋默认值为0select num20;end $$
delimiter ; --将结束符修改成;call test1(); -- 调用存储过程
drop procedure test1 --如果不需要此存储函数开源删除咯
set赋值操作
delimiter $$CREATE PROCEDURE test2 ()
begindeclare num int default 0;set num 50; -- 给num变量赋值select num;end $$
delimiter ;call test2();into的使用方法
delimiter $$
CREATE PROCEDURE test3 ()
begindeclare 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 ()
begindeclare id int default 1; declare class_name varchar(20);if id1 thenset class_name要多久你才可以爱上我;elseif id2 thenset class_name不再让自己遗憾了;elseset 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)
begindeclare class_name varchar(20);if id1 thenset class_name我和xhell脚本的if不一样;elseif id2 thenset class_name我和python中的if语法有一点带你不一样;elseset 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))
begincase when month 1 and month3 thenset seasonspring;when month 4 and month6 thenset seasonsummer;when month 7 and month9 thenset seasonautumn;when month 10 and month12 thenset seasonwinter;end case;
end $$
delimiter ;call test6(9,season); -- 定义会话变量来接收test8存储过程返回的值select season;
xxx代表定义一个会话变量整个会话都可以使用当会话关闭连接断开时销毁
xxx代表定义一个系统变量永久生效。while循环的使用
delimiter $$
CREATE PROCEDURE test7 (in count int)
begindeclare total int default 0;declare i int default 1;while icount doset totaltotali;set ii1;end while;select total;
end $$
delimiter ;call test7(10);repeat的使用
delimiter $$
CREATE PROCEDURE test7 (count int) -- 默认是输入(in)参数
begindeclare total int default 0;repeat set totaltotalcount;set countcount-1;until count0 -- 结束条件,注意不要打分号end repeat;select total;
end $$
delimiter ;call test8(10);使用 select total; 语句输出 total 的最终值。
total 是局部变量只在该存储过程内部有效。
而 total 是用户变量可以在整个会话中使用和共享。 loop的使用
delimiter $$
CREATE PROCEDURE test9 (count int) -- 默认是输入(in)参数
begindeclare total int default 0; sum:loop -- 定义循环标识 set totaltotalcount;set countcount-1;if count 1 thenleave 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()
begincreate temporary table temp_table(id int);insert into temp_table values(1);select * from temp_table;
end;call pro1();
测试存储函数创建临时表
create function fun2()
returns int
begindeclare 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;咱们的业务应该放到咱们的业务层而不是把业务滞留到数据库来处理将业务和数据库严重耦合在一起了这是导致公司开发不使用存储过程的