Mysql应用MySQL存储过程笔记
《Mysql应用MySQL存储过程笔记》要点: 1,创建实例数据库 ?? use db5; ? 2,并插入数据 ?? insert into t values(5); ? 3,创建程序实例 ????? SQL语句存储过程的第一部分是 create procedure ??? language SQL ???? NOT DETERMINISTIC ??? SQL SECURITY DEFINER ??? COMMENT '' ???? select current_date,rand() from t; ? 反映存储过程特性的子句.子句内容在括号之后,主体之前.这些子句都是可选的,他们有什么作用呢 6,Parameters参数 ??? create procedure p5([IN] name data-type)---- //输入参数in可选,默认为参数为in ??? create procedure p5(out name data-type)----- //输出参数out ??? create procedure p5(inout 那么data-type)----- //即可以做输入参数也可以做输出参数 ?? ----输入参数in 例子. ?????? create procedure p5(p int) set @x=p; ?????? call p5(12345); ?????? select @x; ?? ----输出参数out 例子 ?????? create procedure p6(out p int)set p=-5; ?????? call p6(@y); ?????? select @y; ? 7,复合语句:如果你的过程中有多条语句,那么你需要begin/end块.在这里你可以进行变量的定义和流程的控制 ?????? create procedure p7() ?????????? BEGIN ????????????? set? @a=6; ?????? set? @b=5; ?????? insert into t values (@a); ?????? select s1 * @a from t where s1>= @b; ??? END;// ?? -----在复合语句中申明变量 ?????? create procedure p8() ??????????? begin ?????? DECLARE a INT; ?????? DECLARE b INT; ?????? SET a=5; ?????? SET b=5; ?????? insert into t values (a); ?????? select s1*a from t where s1>=b; ??????????? end;// ?????? 含有default默认语句舌设定语句的例子 ?????? create procedure p9() ?????? begin ????????? declare a,b int default 5; ?? insert into t values(a); ?? select s1*a from t where s1>=b; ?????? end;// ? 8,scope作用域的问题:内部的变量在其作用域范围内享有更高的优先权,当执行到end变量时,内部变量消失,此时已经在其作用域外,变量不再可见了,应为在存储 ?? begin ????? declare x1 char(5) default 'outer'; ????? begin ??????? declare x1 char(5) default 'inner'; ?select x1; ????? end; ????? select x1; ?? end;// ?? /*******? 存储过程中的条件式语句? ***********/ 1. if-then -else语句 ??? create procedure p12(in parameter int) ??? begin ????? declare var int; ????? set var=parameter+1; ????? if var=0 then ??????? insert into t values(17); ????? end if; ????? if parameter=0 then ??????? update t set s1=s1+1; ????? else ??????? update t set s1=s1+2; ????? end if; ??? end;// ? 2. case指令:如果需要进行更多条件真假的判断我们可以使用case语句 ??? begin ?????? declare var int; ?????? set var=parameter+1; ?????? case var ???????? when 0 then insert into t values(17); ? when 1 then insert into t values(18); ? else insert into t values(19); ?????? end case; ??? end;// ? /*********? 循环语句? ***********/ ?? begin ???? declare var int; ???? set var=0; ???? while var<6 do ??????? insert into t values(var); ?set var=var+1; ???? end while; ?? end;// ? 欢迎参与《Mysql应用MySQL存储过程笔记》讨论,分享您的想法,编程之家PHP学院为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |