cursor in postgresql example Edit

Murugan Andezuthu Dharmaratnam | 22 September 2021 | 152

here is a sample code to insert data into a table in postgresql using cursor.

Sample Code

declare
    cost_center   record;
    cur_costcenter  cursor for 
		select public.uuid_generate_v4() as reportid,* from costcenter;
begin
	open cur_costcenter;
	loop 
		fetch cur_costcenter into cost_center;
		exit when not found;
		
		
		insert into yourtablename(reportid, environment)
		values(cost_center.reportid, cost_center.costcentername);
		
	end loop;
	close cur_costcenter;
end;