因工作需要,数据库由PostgreSQL 转为Oracle 10g。由于之前的逻辑几乎都分布于存储过程,所以多代码的修改相对来说较小。
因对Oracle 数组参数的转换花了些时间,所以记录下来,分享一下。言归正传:
如果入参为字符串数组、整形数组或者GUID数组等等,并且把它作为一个查询条件,有两种方法可以做到。我采用了package,后面将介绍原因。
方法一:
在存储过程中使用for,相信大家对此应该不陌生。上例子:
create or replace package sbp_disablesyncpreset
as
type string_array is table of raw(16) index by binary_integer;
procedure disablesyncpreset(sync_computers in string_array, active_presets in string_array);
end sbp_disablesyncpreset;
Commit;
create or replace package body sbp_disablesyncpreset
as
procedure disablesyncpreset(sync_computers in string_array, active_presets in string_array)
as
begin
FOR i IN sync_computers.FIRST .. sync_computers.LAST
LOOP
UPDATE sparesync_last_sync SET preset_active='0'
WHERE
((destination_guid=sync_computers(i)) OR (src_guid=sync_computers(i))) AND (NOT preset_id=active_presets(i));
END LOOP;
end disablesyncpreset;
end sbp_disablesyncpreset;
Commit;
方法二:使用自己定义的全局数组类型,上例子:
create or replace package sbp_disablesyncpreset
as
type string_array is table of raw(16) index by binary_integer; //定义数组类型,与入参一致
procedure disablesyncpreset(sync_computers in string_array);//存贮过程名称
end sbp_disablesyncpreset;
Commit;
//主体部分
create or replace package body sbp_disablesyncpreset
as
procedure disablesyncpreset(sync_computers in string_array)
as
computerguids spu_nested_type20 := spu_nested_type20(); //自己定义的全局数组类型
begin
FOR i IN computerid.first..computerid.last loop
computerguids.extend;
computerguids(i) := computerid(i);
end loop;
UPDATE sparesync_last_sync SET preset_active='0'
WHERE
destination_guid in (select column_value from table(Cast(computerguids as spu_nested_type20)))
OR src_gui