Re: Help : Update and insert record based on several value in the parameter - Mailing list pgsql-general

From Tony Shelver
Subject Re: Help : Update and insert record based on several value in the parameter
Date
Msg-id CAG0dhZCZmXUGM2nuNVaL5oNNYC_i=HZHUorjsAxv9LRtc_gHjg@mail.gmail.com
Whole thread Raw
In response to Help : Update and insert record based on several value in the parameter  (Hengky Lie <hengkyliwandouw@gmail.com>)
List pgsql-general
I don't know what you are using as a front end to call this update, but I have been finding json/jsonb parameters passed into a function / procedure and then using postgres json functions to process the data as a good solution. 

If your data fits postgres array datatype, as others have mentioned, that is also a good (and possibly simpler) solution.

On Tue, 29 Jan 2019 at 14:50, Hengky Lie <hengkyliwandouw@gmail.com> wrote:
Hi,

I want to create a function to update my table (flag='9') and insert new record (flag='0') with the rfidnumber specified in a parameter. 

This parameter may have several value seperated by space (ie. 11 22 33 44)

CREATE OR REPLACE FUNCTION public.fcreate_rfid (
  znumber varchar
)
RETURNS boolean AS
$body$
BEGIN
--update old record which has the same rfid number and flag='0' if exists
update tblrfid set flag='9' where flag='0' and rfidnumber in (znumber);
    
    -- generate new record
insert into tblrfid(tanggal, flag, rfidnumber)
select localtimestamp, '0', regexp_split_to_table(znumber, ' ');
    
     return true;
END;
$body$
LANGUAGE 'plpgsql';

when i called this function using command :

select fcreate_rfid('11 22 33 44');

This function fail to update the old record, but success to insert the new record.

Please help me how to fixed this problem. I know the problem is the update command, but i don't know the correct it. Googling anywhere didn't find any solution.

Thank you


Virus-free. www.avg.com

pgsql-general by date:

Previous
From: Sathish Kumar
Date:
Subject: Re: Anonymize Data
Next
From: Fabrízio de Royes Mello
Date:
Subject: Re: Table Replication