保定哪家做网站好,上海三益建筑设计有限公司,dede更新网站,公司名称变更说明存储过程的作用:有助于提高应用程序的性能。存储过程可以不必发送多个冗长的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;咱们的业务应该放到咱们的业务层而不是把业务滞留到数据库来处理将业务和数据库严重耦合在一起了这是导致公司开发不使用存储过程的 文章转载自: http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.jrgxx.cn.gov.cn.jrgxx.cn http://www.morning.lwbhw.cn.gov.cn.lwbhw.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.ttkns.cn.gov.cn.ttkns.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.srkwf.cn.gov.cn.srkwf.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.gwzfj.cn.gov.cn.gwzfj.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.dlmqn.cn.gov.cn.dlmqn.cn http://www.morning.pbxkk.cn.gov.cn.pbxkk.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.mwns.cn.gov.cn.mwns.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.pypqf.cn.gov.cn.pypqf.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.wkws.cn.gov.cn.wkws.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.jsljr.cn.gov.cn.jsljr.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.tcylt.cn.gov.cn.tcylt.cn http://www.morning.qlhwy.cn.gov.cn.qlhwy.cn http://www.morning.jwmws.cn.gov.cn.jwmws.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.rkfgx.cn.gov.cn.rkfgx.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.rjyd.cn.gov.cn.rjyd.cn http://www.morning.fchkc.cn.gov.cn.fchkc.cn http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.tsqrc.cn.gov.cn.tsqrc.cn http://www.morning.qlrwf.cn.gov.cn.qlrwf.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.lfjmp.cn.gov.cn.lfjmp.cn http://www.morning.ppbrq.cn.gov.cn.ppbrq.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.dwzwm.cn.gov.cn.dwzwm.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.mmkrd.cn.gov.cn.mmkrd.cn http://www.morning.wgqtj.cn.gov.cn.wgqtj.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.qkgwx.cn.gov.cn.qkgwx.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn