我有一个存储过程,如下所示:
CREATE OR REPLACE FUNCTION public.sp_shared_asset_delete_by_id(
"in_shared_asset_id" bigint,
"in_client_uuid" uuid)
RETURNS boolean AS
$BODY$
declare
"participant" cursor
begin
"participant" is select * from get_all_participants("in_shared_asset_id");
FOR row in "participant"
LOOP
--DELETE EACH PARTICIPANT
sp_participant_delete_by_id(row.participant_id, row.created_by_client);
END LOOP;
return true;
end
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
get_all_participants(“in_shared_asset_id”)返回一组“参与者”记录。
迭代get_all_participants调用返回的所有记录的正确方法是什么?目前,我得到一个plsql错误“接近”开始。
CREATE OR REPLACE FUNCTION public.get_all_partipants(in_shared_id bigint)
RETURNS SETOF participant AS
$BODY$
begin
return query select participant.participant_id,
participant.shared_asset_id,
participant.participant_role_type,
participant.user_external_ref_uuid,
participant.user_first_name ,
participant.user_last_name ,
participant.user_email_address,
shared_asset.asset_external_ref_uuid uuid,
shared_asset.owner_external_ref_uuid uuid,
participant.deleted_timestamp
from participant
join shared_asset
on shared_asset.shared_asset_id = participant.shared_asset_id
where shared_asset.shared_asset_id = "in_shared_id"
and participant.deleted_timestamp is null;
end
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100
ROWS 1000;
答案 0 :(得分:1)
你甚至不需要游标,但你的plsql语法看起来很奇怪。保持简单。
CREATE OR REPLACE FUNCTION public.sp_shared_asset_delete_by_id(
"in_shared_asset_id" bigint, "in_client_uuid" uuid)
RETURNS boolean IS
DECLARE
row participant%ROWTYPE;
BEGIN
FOR row IN(SELECT * FROM get_all_participants("in_shared_asset_id"))
LOOP
--DELETE EACH PARTICIPANT
sp_participant_delete_by_id(row.participant_id, row.created_by_client);
END LOOP;
RETURN TRUE;
END;
/
答案 1 :(得分:0)
游标定义错误。它应该以{{1}}
结束要使用光标,您需要OPEN
它。
语法;
错误(您在手册中找到那个)
您也没有声明保存游标返回的行的记录变量。您可以通过不使用显式游标变量并简单地loop over the query result
来简化它"participant" is select *...
不相关,但是:CREATE OR REPLACE FUNCTION public.sp_shared_asset_delete_by_id(
"in_shared_asset_id" bigint,
"in_client_uuid" uuid)
RETURNS boolean AS
$BODY$
declare
l_rec record;
begin
"participant" is ;
FOR l_rec in select * from get_all_participants("in_shared_asset_id")
LOOP
--DELETE EACH PARTICIPANT
perform sp_participant_delete_by_id(l_rec.participant_id, l_rec.created_by_client);
END LOOP;
return true;
end
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
可以简化为普通的SQL函数。那里不需要PL / pgSQL。