2012-04-26 64 views
10

我想了解一個Postgres存儲函數如何返回一個具有標識列的表。我用的回報SETOF返回類型:Postgres存儲函數如何返回一個表格

-- 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'; 

,並返回表():

-- 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'; 

但都具有以下格式的輸出:

"(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   ")" 

有反正在其中我可以有輸出,如表中選擇結果的輸出?

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

這樣來自前端的處理結果數據不需要解析器。

回答

11

你應該詢問你的函數是這樣的:

SELECT * FROM employee_search_by_name('Bob'); 

此外,爲了簡化功能,可以觀察到RETURN QUERY EXECUTE ...結構。並且沒有必要引用plpgsql關鍵字。

+0

不需要雙引號。 – 2012-04-26 09:41:09

+0

請檢查下面的答案 – greatkalu 2012-05-01 09:25:36

相關問題