在plpgsql中声明游标失败

问题描述 投票:0回答:1

我在supabase中为plpgsql(postgre)编写了一个数据库函数。

声明游标时我总是得到

ERROR: 42P01: relation "cursor1" does not exist

我是这样声明的:

DECLARE
    cursor1 CURSOR FOR 
        SELECT public.spending_cap.cap, public.spending_cap.spent, public.spending_cap.image_cap, public.spending_cap.image_spent, public.spending_cap.id, public.spending_cap.created_by FROM public.spending_cap, public.user_profile WHERE is_parent = true AND public.user_profile.id = public.spending_cap.created_by AND organisation is NULL;
    entry cursor1%rowtype;

我只是声明

cursor1
怎么能说关系不存在呢?如果我只执行
SELECT
语句,我就会得到结果。我使用这个声明的游标来循环相同的
SELECT
语句。

关于我做错了什么有任何提示吗?

postgresql plpgsql supabase-database
1个回答
0
投票

PL/pgSQL 不允许在游标变量上使用

%ROWTYPE
。 PL/pgSQL的引擎与Oracle有很大不同,它只是脱离了PL/pgSQL的概念。实现这个功能可能不难,但它超出了概念。您应该使用
RECORD
类型来代替:

DECLARE
    cursor1 CURSOR FOR 
        SELECT public.spending_cap.cap, public.spending_cap.spent, public.spending_cap.image_cap, public.spending_cap.image_spent, public.spending_cap.id, public.spending_cap.created_by FROM public.spending_cap, public.user_profile WHERE is_parent = true AND public.user_profile.id = public.spending_cap.created_by AND organisation is NULL;
    entry record;

尝试阅读相关文档。尽管有时语法相似,但在细节上,PL/pgSQL 游标和 PL/SQL 游标之间存在很多差异。

© www.soinside.com 2019 - 2024. All rights reserved.