码迷,mamicode.com
首页 > 其他好文 > 详细

存储过程 游标

时间:2017-12-24 20:05:46      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:.so   存储过程   nbsp   读取   ati   procedure   调用   call   and   

存储过程

查看现有的存储过程
Show procedure status \G;

\G 横着显示
delimiter ;或者$  定义结尾标志;或者$

刪除存储过程
Drop procedure 存儲過程名字

(1)
create procedure p1()
begin
  select * from  s_organization_type;
end$

调用存储过程
Call 存储过程名字();


(2)
create procedure p2(num int)
begin
    select * from s_organization_type a where a.sot_id>num;
end$

call p2(3)  取出主键大于3的信息

(3)
create procedure p3(num int,s char(4))
begin
    if s=‘true‘ then
        select * from s_organization_type a where a.sot_id>num;
    else
        select * from s_organization_type a where a.sot_id<num;
    end if;
end$

call p3(5,‘true‘)
传入的参数s的值为‘true’時,查询主键大于5的语句,否则执行小于5的语句

(4)
create procedure p4(num smallint)
begin
   declare i int default 1;
   declare temp int default 0;
   while i<=num do
     set temp=temp+i;
     set i=i+1;
   end while;
   select temp;
end$


存储过程和函数
procedure function
存储过程没有返回值

函數
create function ..
begin
RETURN 0;
end$


===========================================


游标

 declare continue handler for not found set nomore=0;    
 continue handler 触发后继续执行后面的代码
 exit handler 触发后后面的代码不执行
 undo 触发后后面的代码被撤消

(1)
create procedure p5()
begin
  declare row_sotid int;
  declare row_sotname varchar(20);
  declare row_viewseq tinyint;
 
  declare cnt int default 0;
  declare i int default 0;

  declare cursor_sot cursor for select sot_id,sot_name,view_seq from s_organization_type;   --申明游标

  select count(*) into cnt from s_organization_type;     --給cnt赋值

  open cursor_sot;                                      --打开游标
 
  repeat                          --循环
    set i:=i+1;                        --每次循环增1
    fetch cursor_sot into row_sotid,row_sotname,row_viewseq;  
    select row_sotid,row_sotname,row_viewseq;        --打印数据
  until i>=cnt end repeat;                --停止循环 当i等于count(*)时

  close cursor_sot;                    --关闭游标
 
end$


(2)
create procedure p6()
begin
  declare row_sotid int;
  declare row_sotname varchar(20);
  declare row_viewseq tinyint;

  declare nomore int default 1;
  declare cursor_sot cursor for select sot_id,sot_name,view_seq from s_organization_type;
  declare continue handler for not found set nomore=0;        --当读取不到数据时把nomore设为0,然后停止循环


  open cursor_sot;

  while nomore != 0 do                        

    fetch cursor_sot into row_sotid,row_sotname,row_viewseq;
    select row_sotid,row_sotname;
    
  end while;
  close cursor_sot;
 
end$


(3)
CREATE PROCEDURE `p6`()
begin
  declare row_sotid int;
  declare row_sotname varchar(20);
  declare row_viewseq tinyint;

  declare nomore int default 1;
  declare cursor_sot cursor for select sot_id,sot_name,view_seq from s_organization_type;
  declare continue handler for not found set nomore=0;
 
  open cursor_sot;

 
while nomore<>0 do

        fetch cursor_sot into row_sotid,row_sotname,row_viewseq;
    if nomore <> 0 then
        
      select row_sotid,row_sotname;
            
    end if;
        
end while;

  close cursor_sot;
 
    select ‘1‘;
    
    
end






存储过程 游标

标签:.so   存储过程   nbsp   读取   ati   procedure   调用   call   and   

原文地址:http://www.cnblogs.com/m97i/p/8098721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!