行mod 3的PL / pgSQL游标

时间:2016-04-20 23:20:07

标签: sql postgresql cursor plpgsql

所以我在PL / pgSQL中工作,我想创建一个只返回行%3 = 0行的数据的函数,我需要使用游标。

我有以下但显然不起作用

create or replace function punto6() returns void AS $$
declare
    rPers personas%ROWTYPE;             
    curs3   CURSOR  IS  select * from personas order by nombres;
BEGIN   
        FOR rPers   IN  curs3   LOOP
        if current of curs3 % 3 =0  THEN
            RAISE   NOTICE  ''Nombre:   %   %'',    rPers.nombres,  
            rPers.apellidos;
        END LOOP;                           
        return;         
end;
$$  LANGUAGE plpgsql;   

1 个答案:

答案 0 :(得分:0)

create or replace function punto6() returns void AS $$
declare
    rPers RECORD;               
    curs3   CURSOR  IS  select * from personas order by nombres;
    numero int;
BEGIN
    numero = 1;
    OPEN curs3;
    LOOP
    FETCH curs3 INTO rPers;
    EXIT WHEN NOT FOUND;
    if numero % 3 = 0   THEN
        RAISE   NOTICE  'Nombre:    %   %', rPers.nombres,  
        rPers.apellidos;
    END IF;
    numero = numero + 1;
    END LOOP;
    CLOSE curs3;
    return;         
end;
$$  LANGUAGE plpgsql;