i'm writing postgres function should delete 3 tables successively.
the relation delete mobgroupdata -> mobilenums -> terminals , when don't have element in mobgroupdata, want delete mobilenums , terminals. should condition. i've tried
if mrec.id != 0, didn't work, i've tried exists, didn't work. when made select statement db , mobgroupdata's id doesn't exist, code breaking, when select element consist in tables works. know should if statement make works?
create or replace function "delete_from_terminals_casc_final12"( "id_list" bigint, "curuser_id" bigint) returns setof term_mgd_mobnums $body$ declare mrec "term_mgd_mobnums"%rowtype; begin mrec in select mn."id_terminals", t.sn , t.imei ,t.les ,t.category ,t.model ,t.tswv ,t.status ,t.activation_date ,t.deactivation_date ,t.paytype ,t.ip_address ,t.pin1 ,t.pin2 ,t.puk1 ,t.puk2 ,t.notes ,t.units ,t.validtill, t.responsible_user,t.id_clients,t.currentuser, t.isn, md.id_mobilenums, mn.current_status, mn.start_date ,mn.streason ,mn.unit ,mn.mobnumber ,mn.service ,mn.status mn_status,mn.activator ,mn.responsible_department,mn.date_changed ,mn.reason ,mn.installed_on ,mn.usedby ,mn.regnumber ,mn.responsible_user mn_responsible_user ,mn.description, md.id,md.les1 ,md.les2,md.les3,md.les4,md.les5,md.member1 ,md.member2,md.member3,md.member4,md.member5,md.user1 ,md.user2,md.user3,md.user4,md.user5,md.pass1 ,md.pass2,md.pass3,md.pass4,md.pass5 terminals t inner join mobilenums mn on t."id" = mn."id_terminals" inner join mobgroupdata md on md."id_mobilenums" = mn."id" mn."id_terminals" = $1 loop if exists perform "delete_from_mobgroupdata2"(mrec.id,$2); perform "delete_from_mobilenums"(mrec.id_mobilenums::text,$2); perform "delete_from_terminals"(mrec.id_terminals::text,$2); else perform "delete_from_mobilenums"(mrec.id_mobilenums::text,$2); perform "delete_from_terminals"(mrec.id_terminals::text,$2); end if; return next mrec; end loop; return; end;$body$ language plpgsql volatile cost 100 rows 1000; alter function "delete_from_terminals_casc_final12"(bigint, bigint) owner postgres;
two problems code, if reading question correctly:
- you using
inner joinjoinmobgroupdata. retrieve results rows exist in of tables. useleft outer joininstead. - you tried
mrec.id != 0, lookingnull, not0.0,nullnot same thing in sql. condition wantmrec.id not null.
Comments
Post a Comment