遊標的作用及屬性
遊標的作用就是用來對查詢資料庫所傳回的記錄進行遍歷,以便進行對應的操作;遊標有以下這些屬性:
a、遊標是唯讀的,也就是不能更新它;
b、遊標是不能滾動的,也就是只能在一個方向上進行遍歷,不能在記錄之間隨意進退,不能跳過某些記錄;
c、避免在已開啟遊標的表格上更新資料。
實作功能,將資料量比較大的nt_m_gpsdata(3000W+),依日期分割成如nt_m_gpsdata20170501,nt_m_gpsdata20170502,nt_m_gpsdata20170503等
##F1
##-- 提取遊標裡的數據,這裡只有一個,多個的文字也一樣;
FETCH cur INTO a;
-- 宣告結束的時候
IF done THEN
LEAVE read_loop;
END IF;
-- 這裡做你想做的循環的事件
set tbname=CONCAT("nt_m_gpsdata",a);#
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;
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中文網其他相關文章!