oracle 遊標使用

遊標

遊標能夠理解爲一個查詢集合;使用時先申明遊標; declare cursor cursorName is select子句; 使用時用for循環調用便可;函數

for xx in  cursorName  loop
 doSomething
end loop;

這樣就能夠了;xx表明select結果的一條記錄,使用xx.id就表示要取該記錄的id字段值;下面是一個存過完整的例子oop

declare 
  cursor mainTask is  select m.id  from sys_monthtask_main m
  left join sys_month_task_basicdata d on m.basdataid=d.id
  where d.month='2016-07' ;
begin
  for task in mainTask loop
  update sys_monthtask_main m set m.tal4done=(select count(1) from sys_monthtask_sub s where s.parentid=task.id and s.goal=4 and s.done>=s.goal) where m.id=task.id;
  update sys_monthtask_main m set m.tal7done=(select count(1) from sys_monthtask_sub s where s.parentid=task.id and s.goal=7 and s.done>=s.goal) where m.id=task.id;
  update sys_monthtask_main m set m.tal10done=(select count(1) from sys_monthtask_sub s where s.parentid=task.id and s.goal=10 and s.done>=s.goal) where m.id=task.id;
  update sys_monthtask_main m set m.tal15done=(select count(1) from sys_monthtask_sub s where s.parentid=task.id and s.goal=15 and s.done>=s.goal) where m.id=task.id;
  update sys_monthtask_main m set m.tal20done=(select count(1) from sys_monthtask_sub s where s.parentid=task.id and s.goal=20 and s.done>=s.goal) where m.id=task.id;
  end loop;
  commit;
end;

字符串補全函數

select  'epu-'||replace(lpad('123',7),' ','0') as orderno from dual;

123左邊補全7位,不夠加0code

select * from students where instr(address, 'beijing') > 0 字符串中包含'beijing';字符串

select * from trunc(og1.created + 1, 'iw')it

  • 截取每週一的時間
相關文章
相關標籤/搜索