張 zi 浩
发布于

OushuDB 游标使用

create or replace function cursor_demo()
returns void as
$$
declare
    unbound_refcursor refcursor;
    v_id int;
    v_step_desc varchar(1000);
begin
    open unbound_refcursor for execute 'select c_custkey,c_address from customer order by c_custkey limit 100';
    drop table if exists public.cursor_insert_table;
    create table public.cursor_insert_table(id int,step_desc text);
    loop
        fetch unbound_refcursor into v_id,v_step_desc;
        if found then
            raise notice '%-%',v_id,v_step_desc;
            insert into cursor_insert_table values(v_id,v_step_desc);
        else
            exit;
        end if;
    end loop;
    close unbound_refcursor;
    raise notice 'the end of msg...';
exception when others then
    raise exception 'error--(%)',sqlerrm;
end;
$$
  LANGUAGE plpgsql;
select cursor_demo();
评论(1)
test