游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作;游标有下面这些属性:
a、游标是只读的,也就是不能更新它;
b、游标是不能滚动的,也就是只能在一个方向上进行遍历,不能在记录之间随意进退,不能跳过某些记录;
c、避免在已经打开游标的表上更新数据。
实现功能,将数据量比较大的nt_m_gpsdata(3000W+),按日期拆分成如nt_m_gpsdata20170501,nt_m_gpsdata20170502,nt_m_gpsdata20170503等
CREATE PROCEDURE `new_procedure` ()
BEGIN
-- 需要定义接收游标数据的变量
DECLARE a CHAR(16);
-- 定义新建表名
DECLARE tbname CHAR(30);
-- 定义存放sql语句的变量
DECLARE sqlstr1 varchar(300);
DECLARE sqlstr2 varchar(300);
-- 遍历数据结束标志
DECLARE done INT DEFAULT FALSE;
-- 定义游标
DECLARE cur CURSOR FOR select DISTINCT DATE_FORMAT(ctime,'%Y%m%d') as ctime from nt_m_gpsdata;
-- 将结束标志绑定到游标
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- 打开游标
OPEN cur;
-- 开始循环
read_loop: LOOP
-- 提取游标里的数据,这里只有一个,多个的话也一样;
FETCH cur INTO a;
-- 声明结束的时候
IF done THEN
LEAVE read_loop;
END IF;
-- 这里做你想做的循环的事件
set tbname=CONCAT("nt_m_gpsdata",a);
-- select tbname;
-- 复制表结构,create table newtable select * from oldtable where 1=2 只能复制表字段,无法复制字段主键、自增、非空等属性 create table newtable like oldtable 可以复制字段属性
set sqlstr1 = CONCAT("create table ",tbname," like nt_m_gpsdata"); set sqlstr2 = CONCAT("insert into ",tbname," select * from nt_m_gpsdata where deleted=0 and DATE_FORMAT(ctime,'%Y%m%d')='",a,"'"); set @firstsql = sqlstr1; PREPARE stmt1 FROM @firstsql; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; set @secondsql = sqlstr2; PREPARE stmt2 FROM @secondsql; EXECUTE stmt2; DEALLOCATE PREPARE stmt2;
END LOOP;
-- 关闭游标
CLOSE cur;
END
后来又将这个分表策略应用到一个oracle项目,附上代码
DECLARE cursor my_cursors is select DISTINCT to_char(ctime,'yyyymmdd') as ctime from NTGIS_GPS_EVENTDATA; mcursor varchar2(40); begin for mcursor in my_cursors loop DECLARE tbname VARCHAR2(50) := 'NTGIS_GPS_EVENTDATA' mcursor.ctime; sqlstr VARCHAR2(300) := 'CREATE TABLE ' tbname ' as SELECT * from NTGIS_GPS_EVENTDATA where to_char(ctime,''yyyymmdd'')=''' mcursor.ctime ''''; BEGIN --dbms_output.put_line(tbname); execute immediate sqlstr; END; end loop; end;
以上就是游标的作用及属性介绍的详细内容,更多请关注php中文网其它相关文章!
……