Thursday, October 21, 2021

Sample pgsql function and its usage

--First, run this ode, it will create a function
create function update_user_groups(userId int)
returns void
language plpgsql
as
$$
DECLARE
 i  INTEGER := (select max(sort_order) from user_groups where user_id=userId);
begin
for r in 1000006..1000026 loop
i = i+1;
insert into user_groups values(userId, r, i) ON CONFLICT DO NOTHING;
end loop;
end;
$$;
--If everything ok, this will create a function in your functions list


--Then call the function u created giving arguments (ex userId in this case)
select update_user_groups(103);

No comments: