您现在的位置: 捷凌网安 >> 服务器 >> 数 据 库 >> MYSQL >> 正文
MySQL存储过程示例

作者:佚名 责任编辑:左决 点击数: 更新时间:2008-4-14 3:46:28

 今天写了第一个使用游标的mysql存储过程,下面是这个存储过程的内容:

  delimITer $$
  create procedure sp_poster()
  begin
  declare id int; --声明变量
  declare cnt int; --声明变量
  declare cur_channel_id cursor for select id from channel; --声明游标
  declare EXIT HANDLER FOR NOT FOUND CLOSE cur_channel_id; --捕获游标NOT FOUND的条件,循环结束,并关闭游标
  select count(*) into cnt from channel;
  if cnt <> 0 then
  open cur_channel_id; --打开游标
  repeat
  fetch cur_channel_id into id; --移动游标
  insert into POSTER_MONTH(id, gmt_create, gmt_modified, title, short_tITle, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs)
  select id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs
  from poster where gmt_create >= date_add(now(),interval -30 day)
  and channel_id = id
  order by hits desc limIT 10;
  insert into POSTER_WEEK(id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs)
  select id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs
  from poster where gmt_create >= date_add(now(),interval -7 day)
  and channel_id = id
  order by hits desc limIT 10;
  commIT;
  until 0 end repeat;
  close cur_channel_id;
  insert into POSTER_MONTH(id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs)
  select id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs
  from poster where gmt_create >= date_add(now(),interval -30 day)
  order by hits desc limIT 10;
  commIT;
  insert into POSTER_WEEK(id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs)
  select id, gmt_create, gmt_modified, title, short_title, user_id, user_nick, tags,channel_id,weight,cover_pic_path,hITs
  from poster where gmt_create >= date_add(now(),interval -7 day)
  order by hits desc limIT 10;
  commIT;
  end if;
  end $$

 存储过程本身与oracle的存储过程并没有太大的差异,写起来也比较顺手的。其中一点要注意:SQL语句,对时间的处理,mysql与oracle就有很大的不同。在oracle里,时间可以直接相减,比如:

  14:12:38 SQL> select sysdate - 5 from dual;
  SYSDATE-5
  -------------------
  2008-03-29 14:12:49
  1 row selected.

  但是在mysql里面,要使用date_add函数去完成(具体语法,在网上google一下)

  mysql> select date_add(now(),interval -5 day);
  +---------------------------------+
  | date_add(now(),interval -5 day) |
  +---------------------------------+
  | 2008-03-29 14:12:49 |
  +---------------------------------+
  1 row in set (0.00 sec)

  上面的时间函数使用起来复杂一点,但只要能完成这个功能就可以了!

  • 上一篇文章:

  • 下一篇文章:
  •  
    最进更新
    普通文章foxpro 更新源表05-05
    普通文章foxpro 让视图与数据源相连05-05
    普通文章foxpro 机动查询和数据输入05-05
    普通文章foxpro 多个本地数据05-05
    普通文章foxpro 维护源表05-05
    普通文章Oracle 10g Release2新功能之05-05
    普通文章将Oracle 10g内置的安全特性05-05
    普通文章ACCESS 2003 建立数据库视频05-05
    普通文章三种SQL分页法效率分析05-05
    普通文章优化MySQL数据库查询的三种方05-05
     
    推荐文章
    推荐文章教你怎样在MySQL中提高全文搜05-05
    推荐文章SQL Server中数据导入导出三05-05
    推荐文章缓冲技术提高JSP程序的性能和04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    推荐文章何时使用DataGrid、DataList04-17
    推荐文章MySQL存储过程示例04-14
    推荐文章华硕搭建Exchange2007企业邮03-14
    推荐文章升级Win 2003到Windows 200803-14
    推荐文章windows Server 2003 搭建域03-14
    推荐文章服务器成为IT中心的6个理由03-11
     
    热点文章 
    普通文章SQL Server 2008分析服务概览05-01
    普通文章Dlink路由器VPN设置04-29
    推荐文章缓冲技术提高JSP程序的性能和04-17
    普通文章教你优化你的ASP程序04-17
    推荐文章asp去除HTML标记的三个实用函04-17
    普通文章ASP添加验证码的解决方法04-17
    推荐文章何时使用DataGrid、DataList04-17
    普通文章Asp.net中禁止用户多次登录04-17
    普通文章MySQL之表结构修改04-14
    推荐文章MySQL存储过程示例04-14

    | 设为首页 | 加入收藏 | 联系站长 | 广告服务 | 友情链接 | 版权申明 | 网站地图 |

    在线交流 捷凌网安主群:51649627
    Copyright 2007-2008 © 捷凌网安. All rights reserved.
    备案序号:蜀ICP备08001812号