2012-04-26 20 views
10

Me gustaría saber cómo una función almacenada de Postgres puede devolver una tabla, con columnas identificadas. retornos que he usado SETOF returnType:Cómo puede una función almacenada de Postgres devolver una tabla

-- create employeeSearchResult returnType 
create type employeeAllReturnType as 
(
    id bigserial, 
    "positionId" integer, 
    "subjectId" bigint, 
    "dateEngaged" date, 
    "nextKin" text, 
    "nrcNo" text, 
    dob date, 
    father text, 
    mother text, 
    wife text, 
    "userId" integer, 
    "statusId" integer, 
    "mainCode" text, 
    "subCode" text 
); 


-- Search for emmployee by name 
CREATE OR REPLACE FUNCTION "employee_search_by_name"(employeeNameIN text) 
returns setof employeeAllReturnType as 
$$ 
declare 
    results record; 
    resultsRow employee%rowtype; 
    nameIn text; 
begin 
    nameIn = employeeNameIN || '%'; 
    for results in select 
     employee.id,-- bigserial NOT NULL, 
    employee."positionId",-- integer, 
    employee."subjectId",-- bigint NOT NULL, 
    employee."dateEngaged",-- date, 
    employee."nextKin",-- text, 
    employee."nrcNo",-- text, 
    employee.dob,-- date, 
    employee.father,-- text, 
    employee.mother,-- text, 
    employee.wife,-- text, 
    employee."userId",-- integer NOT NULL, 
    employee."statusId",-- integer, 
    employee."mainCode",-- character(5) NOT NULL, 
    employee."subCode"-- character(10), 
    from employee, subject where employee."subjectId" = subject.id and (subject.name1 ILIKE nameIn OR subject.name2 ILIKE nameIn OR subject.name3 ILIKE nameIn OR subject.name4 ILIKE nameIn) loop 
     return next results; 
    end loop; 
end; 
$$ language 'plpgsql'; 

y también la tabla de retorno():

-- Search for emmployee by name 
CREATE OR REPLACE FUNCTION "employee_search_by_name"(employeeNameIN text) 
returns table (id bigserial, 
    position integer, 
    subject bigint, 
    date_engaged date, 
    next_kin text, 
    nrc_no text, 
    dob date, 
    father text, 
    mother text, 
    wife text, 
    user_id integer, 
    status_id integer, 
    main_code text, 
    sub_code text) as 
$$ 
declare 
    results record; 
    resultsRow employee%rowtype; 
    nameIn text; 
begin 
    nameIn = employeeNameIN || '%'; 
    for results in select 
     employee.id,-- bigserial NOT NULL, 
     employee."positionId",-- integer, 
     employee."subjectId",-- bigint NOT NULL, 
     employee."dateEngaged",-- date, 
     employee."nextKin",-- text, 
     employee."nrcNo",-- text, 
     employee.dob,-- date, 
     employee.father,-- text, 
     employee.mother,-- text, 
     employee.wife,-- text, 
     employee."userId",-- integer NOT NULL, 
     employee."statusId",-- integer, 
     employee."mainCode",-- character(5) NOT NULL, 
     employee."subCode"-- character(10), 
    from employee, subject where employee."subjectId" = subject.id and (subject.name1 ILIKE nameIn OR subject.name2 ILIKE nameIn OR subject.name3 ILIKE nameIn OR subject.name4 ILIKE nameIn) loop 
     return next results; 
    end loop; 
end; 
$$ language 'plpgsql'; 

Pero ambos tienen salidas en los siguientes formatos:

"(1,1,1,2011-12-01,Timea,fg1254,1981-12-27,moses,sarada,timea,1,1,"ADM ","1   ")" 
"(37,3,10,2011-11-11,s,s,2011-11-11,s,s,s,1,1,"OP ","1   ")" 

¿Hay alguna forma en la que Puedo tener salidas como las de un resultado de selección de una tabla?

"1";1;1;"2011-12-01";"Timea";"fg1254";"1981-12-27";"moses";"sarada";"timea";1;1;"ADM ";"1   " 

Tal que el manejo de los resultados del frente no requiere un analizador.

Respuesta

11

Usted debe consultar a su función como esta:

SELECT * FROM employee_search_by_name('Bob'); 

Además, para simplificar su función, se puede intentar la construcción RETURN QUERY EXECUTE .... Y no es necesario citar la palabra clave plpgsql.

+0

No es necesario que aparezcan las comillas dobles allí. –

+0

Por favor, compruebe la respuesta a continuación – greatkalu

Cuestiones relacionadas