13_什么是存储过程?

如何创建存储过程?

       create procedure 名字()    begin ....... end

         .......中写入对数据库的操作

       -- 调用存储过程   call 名字()

       -- 删除存储过程   drop procedure 名字

       -- 如何修改?

              -- 先删除,再创建

              -- drop procedure if exist 名字         # 判断该存储过程是否存在

       命令规则  proc_操作名

    存储过程有点像写python函数,MySQL把分号当做结束符

如何定义mysql结束符号?

       delimiter $$          # 声明以$$作为结束符号

       delimiter ;             # 再次声明;作为结束符

存储过程的框架是什么?

       delimiter $$                  -- 声明结束符号$$

       drop procedure if exists 存储过程名字$$

       create procedure 存储过程名字(

    in i1 int,out i2 int,inout i3 int       

        -- in定义传入参数,out表示从存储过程中获取值,inout是形参也是获取值对象

    )

  begin

       declare d1 int;            -- 定义整形变量d1

       declare d2 int default 3;         -- 定义整形变量d2,设置默认参数3

       set d1 = i1 + d2;                       -- 给d1设置值

       select * form man_to_wommen where nid > d1     -- 执行sql语句

  end

  delimiter ;                                -- 再次声明 ; 作为结束符

      -- 在sql中,in声明形参,declare声明变量名,set给变量赋值,default给变量设置默认值

如何获取输出的值?

  set @接收参数 = 值; 对inout类型进行传参

  call 存储过程名字(实参1,@接收参数);

  select @接收参数;

存储过程中的分支?

       if 条件 then 函数体

       elseif 条件 then 函数体

       else then 函数体

       end if

存储过程能拿到什么结果?

       -- sql语句执行结果

       -- out inout 定义后设置的值

如何把查询到的结果写入一个变量中?

       -- into          --

       -- select man_id into 变量 where 条件     -- 等价于 set 变量 = 查询结果man_id

       -- 变量能接收什么值,要看变量的类型

原文地址:https://www.cnblogs.com/2bjiujiu/p/7003769.html