Re: Help : Update and insert record based on several value in the parameter

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Hengky Lie <hengkyliwandouw(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Help : Update and insert record based on several value in the parameter
Date: 2019-01-29 13:02:23
Message-ID: CAFj8pRC6zo-n42GDVo==oxw=DOUfuscFUMnVzS36sFAnTF0ULA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

út 29. 1. 2019 v 13:50 odesílatel Hengky Lie <hengkyliwandouw(at)gmail(dot)com>
napsal:

> 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)
>

For this kind of parameters, PostgreSQL has a array type - for example a
int[] can be perfect for this purpose

If you still use a this format, then you can use a string_to_array function
and searching in array

so your UPDATE command should to look like

UPDATE tblrfid SET flag = 9 WHERE flag = '0' and rfidnumber =
ANY(string_to_array(znumber))

Regards

Pavel

> 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
>
>
>
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail> Virus-free.
> www.avg.com
> <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#m_-6825970932945715657_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Abdullah Al Maruf 2019-01-29 13:13:11 pg_rewind success even though getting error 'record with incorrect prev-link'
Previous Message Andrew Gierth 2019-01-29 13:02:13 Re: Help : Update and insert record based on several value in the parameter