将带有“Connect By”和“TYPE”的Oracle存储过程转换为MySQL

时间:2012-10-05 22:38:12

标签: mysql oracle cursor

我很难将oracle SP转换为MySQL中的那个。特别是,对于" Connect by"和" TYPE"。

以下是使用Oracle存储过程解决的问题。

有一个包含3列的表

ID  |Last_Modified | Flag   
1   |2011-12-11 02:00:00|   0   
2   |2011-12-13 02:00:00|   0   
3   |2011-12-02 02:00:00|   0 
4   |2011-12-12 02:00:00|   0 

存储过程“getback(String pId,String pLast_Modified)。例如。 pId =“1,2,3”和pLast_Modified =“2011-12-11 02:00:00,2011-12-13 02:00:00,2011-12-01 02:00:00”。

逻辑是,对于相同的ID,如果表中的last_modified晚于传递的last_modified,则将该记录的标志设置为“1”。

这是Oracle存储过程:

create or replace procedure test1(
p_ids varchar2,   
p_dates varchar2  
) IS

TYPE id_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
TYPE date_type IS TABLE OF DATE INDEX BY VARCHAR2(64);

ids id_type;
last_updated date_type;
lvar1 varchar2(200);
lvar2 varchar2(200);



cursor c1 is 
With ids as (select rownum id, regexp_substr(p_ids,'[^,]+', 1, level) val from dual
connect by regexp_substr(p_ids, '[^,]+', 1, level) is not null), 
lupdated as (select rownum id, regexp_substr(p_dates,'[^,]+', 1, level) val from dual
connect by regexp_substr(p_dates, '[^,]+', 1, level) is not null)
select lut.id, 
case when lut.last_updated > to_date(lu.val,'yyyy-mm-dd hh24:mi:ss') then 1
          else 0
end flag
from last_updated_tbl lut,ids,lupdated lu
where lut.id = ids.val
and   ids.id = lu.id
;

BEGIN
open c1;
loop
 fetch c1 into lvar1,lvar2;
 exit when c1%notfound;

 dbms_output.put_line(lvar1||' flag:'||lvar2);

end loop;

close c1;

END;

0 个答案:

没有答案