create or replace function getUserByUnit(ccmsOriginId in number,node
indexId in varchar2)oop
return varchar2 isget
str varchar2(4000) default '';it
i number default 0;io
beginfunction
for x in (select s.user_idselect
from t_sys_unit u, t_sys_staff snio
where u.ccms_origin_id = ccmsOriginIdcms
and u.node_id = s.org_node_idco
and not exists (select c.alert_config_id
from T_YJ_ALERT_CONFIG c
where c.unit_id = ccmsOriginId
and c.staff_id = s.user_id
and c.index_id = indexId)) Loop
if i <> 0 then
str := str || ',';
end if;
str := str || x.user_id;
i := i + 1;
end Loop;
return(str);
end getUserByUnit;
使用:
select t.unit_id,
t.staff_id,
t.index_id,
t.alert_value1,
t.alert_value2,
'1' message_type
from T_YJ_ALERT_CONFIG t
where t.staff_id is not null
union all
select t.unit_id,
getuserbyunit(t.unit_id, t.index_id) staff_id,
t.index_id,
t.alert_value1,
t.alert_value2,
'2' message_type
from T_YJ_ALERT_CONFIG t
where t.staff_id is null