2012-04-26 6 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

二重引用符は必要ありません。 –

+0

以下の答えを確認してください – greatkalu

関連する問題