Thursday, September 15, 2016

Inserting data from a table to another table using cursor

--There 4 actions in working with cursor
--declare the cursor
--open the cursor
--fetch the data from cursor and use the data in program statements
--close the cursor
create or replace function printtestbst()
    returns void as $$
declare
    c_testbst scroll cursor for
        select id, parentId, value from testbst;   
    rw testbst%rowtype;
begin
    delete from testreportbst;   
    open c_testbst;
    loop
        fetch c_testbst into rw;
        exit when not found;       
        insert into testreportbst values(rw.id, rw.parentId, rw.value, 'A');
    end loop;
      
    close c_testbst;   
end $$language plpgsql;

No comments:

Post a Comment