简体   繁体   中英

How can I check if an element in a table exists?

I'm writing a Postgres function which should delete from 3 tables successively.

The relation is delete from mobgroupdata -> mobilenums -> terminals and when I don't have an element in mobgroupdata, I want to delete from mobilenums and then from terminals. But what should be the condition. I've tried with

IF mRec.id != 0 , but it didn't work, than I've tried with exists , it also didn't work. Also when I made my select statement from the DB and mobgroupdata's id doesn't exist, the code is breaking, but when I select element which consist in all tables it works. Does anybody know what should be the if statement to make it works?

CREATE OR REPLACE FUNCTION "Delete_From_Terminals_Casc_final12"(
"Id_list" bigint,
"Curuser_id" bigint)
  RETURNS SETOF term_mgd_mobnums AS
$BODY$
declare
  mRec "term_mgd_mobnums"%ROWTYPE;

BEGIN

    for 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 as mn_status,mn.activator ,mn.responsible_department,mn.date_changed ,mn.reason ,mn.installed_on ,mn.usedby ,mn.regnumber ,mn.responsible_user as 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 from terminals t
inner join mobilenums mn on t."id" = mn."id_terminals"
inner join mobgroupdata md on md."id_mobilenums" = mn."id"
where mn."id_terminals" = $1  
loop    

 IF exists THEN
 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 TO postgres;

Two problems with your code, if I am reading your question correctly:

  1. You are using INNER JOIN to join to mobgroupdata . This will only retrieve results for rows which do exist in all of your tables. Use LEFT OUTER JOIN instead.
  2. You tried mRec.id != 0 , but you are looking for NULL , not 0 . 0 and NULL are not the same thing in SQL. The condition you want is mRec.id IS NOT NULL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM