postgresql - How to make a conditional query in Postgres function -


i'm starting postgress, , need make query 4 ids params, these can null, first mandatory, i'm trying data list data of every id. how can check if id param not null inside condition. i'll give example (not working)

create or replace function sp_lock(     in p_id_1 character varying,      in p_id_2 character varying default null,      in p_id_3 character varying default null,      in p_id_4 character varying default null) returns setof character varying $body$ begin      update stock     set lock = 1      (       select s.id        stock s               (ls.id = p_id_1 or p_id_1 null) or         (ls.id = p_id_2 or p_id_2 null) or         (ls.id = p_id_3 or p_id_3 null) or         (ls.id = p_id_4 or p_id_4 null) ,         ls.lock = 0       update of s     )     i.id = stock.id; 

here need check first, if param not null, , concatenate condition, or exp.

your can use in operator in condition

where      s.id in (p_id_1, p_id_2, p_id_3, p_id_4) ,     s.lock = 0; 

btw, not need subquery:

update stock set lock = 1     id in (p_id_1, p_id_2, p_id_3, p_id_4) ,     lock = 0; 

does same quicker.


Comments