PostgreSQL에서 새로 도입되었습니다. 방금 복합 유형으로 함수를 만들었습니다. Postgresql은 열 이름이 아닌 값만 반환하는 함수를 저장합니다.
다음
내 코드입니다 :create type search_type as (
result_id bigint,
result_name character varying,
result_loc character varying,
result_type character varying,
result_pic character varying
);
CREATE OR REPLACE FUNCTION search_result("@search_text" character varying)
RETURNS SETOF search_type AS
$BODY$
DECLARE r search_type%rowtype;
BEGIN
FOR r IN SELECT "User_id" AS result_id, CONCAT("User_firstname", ' ', "User_lastname") AS result_name, "User_loc" AS result_loc, "User_picurl" AS result_pic, 'user' AS result_type FROM users_list WHERE CONCAT("User_firstname", ' ', "User_lastname") LIKE CONCAT("@search_text", '%') LOOP
RETURN NEXT r;
END LOOP;
FOR r IN SELECT "Community_id" AS result_id, "Community_name" AS result_name, "Community_location" AS result_loc, "Community_img_url" AS result_pic, 'community' AS result_type FROM community_list WHERE "Community_name" LIKE CONCAT("@search_text", '%') AND "Type" = 'Created' LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END
$BODY$
LANGUAGE plpgsql
하지만 아래의 코드처럼 저장 함수를 호출 할 때, 그것은 것입니다에만 열 이름하지 datas.
SELECT search_result('c');
어떤 이유가 있을까요?
어떻게 열 이름을 값으로 반환 할 수 있습니까?
위의 내용은 반환 된 recorsd입니다.
result_id result_name result_loc result_pic result_type
--------- ----------- ---------- ----------- ----------
13 test1 San Francisco user
14 test2 San Francisco user
15 test3 San Francisco user
16 test4 San Francisco user
17 test5 San Francisco user
어떻게 내가 위의 같은 결과를 얻을 수 있습니다
하지만 난 같은 결과를 얻을 필요가?
UPDATE
새로운 코드 :
CREATE OR REPLACE FUNCTION search_result("@search_text" character varying, "@row_start" integer, "@row_end" integer)
RETURNS SETOF search_type AS
$BODY$
DECLARE r search_type%rowtype;
BEGIN
FOR r IN SELECT "User_id" AS result_id, CONCAT("User_firstname", ' ', "User_lastname") AS result_name, "User_loc" AS result_loc, "User_picurl" AS result_pic, 'user' AS result_type FROM users_list WHERE CONCAT("User_firstname", ' ', "User_lastname") LIKE CONCAT("@search_text", '%') ORDER BY CONCAT("User_firstname", ' ', "User_lastname") ASC LIMIT "@row_end" OFFSET "@row_start" LOOP
RETURN NEXT r;
END LOOP;
FOR r IN SELECT "Community_id" AS result_id, "Community_name" AS result_name, "Community_location" AS result_loc, "Community_img_url" AS result_pic, 'community' AS result_type FROM community_list WHERE "Community_name" LIKE CONCAT("@search_text", '%') AND "Type" = 'Created' ORDER BY "Community_name" ASC LIMIT "@row_end" OFFSET "@row_start" LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END
$BODY$
LANGUAGE plpgsql
그럼 내가 같은 절차라고 :
SELECT * FROM search_result('t',3,0)
을하지만, 더 값이 반환되지 않습니다.
@a_horse_with_no_name 방금 내 질문을 변경했습니다 .... – Kichu
괜찮아요. – Kichu