如何从一个函数postgres返回多个值
我有这两个函数正在返回光标并动态创建文本,但是我需要在一个函数中执行。
CREATE OR REPLACE FUNCTION get_students2(classId integer)
RETURNS refcursor
LANGUAGE plpgsql
AS
$$
DECLARE
students text DEFAULT '';
student record;
cursor_students CURSOR(classId integer)
FOR SELECT firstName, surname
FROM tests.students
WHERE class = classId;
BEGIN
OPEN cursor_students(classId);
LOOP
FETCH cursor_students INTO student;
EXIT WHEN NOT FOUND;
students := students || ' ' || student.firstName || ' ' || student.surname;
END LOOP;
CLOSE cursor_students;
RETURN cursor_students;
END;
$$
CREATE OR REPLACE FUNCTION get_students(classId integer)
RETURNS refcursor
LANGUAGE plpgsql
AS
$$
DECLARE
students text DEFAULT '';
student record;
cursor_students CURSOR(classId integer)
FOR SELECT firstName, surname
FROM tests.students
WHERE class = classId;
BEGIN
OPEN cursor_students(classId);
LOOP
FETCH cursor_students INTO student;
EXIT WHEN NOT FOUND;
students := students || ' ' || student.firstName || ' ' || student.surname;
END LOOP;
CLOSE cursor_students;
RETURN students;
END;
$$
我试图找到如何做,但没有遇到任何解决方案。我考虑过将其制作在桌子里,但我不知道这是否可能,并且什么也没发现。
我根本没有经验丰富的SQL,所以不知道是否可以使用这种事情。
这是一项任务,规则是需要一个光标,动态SQL和函数必须返回两者。
谢谢。
I have these two functions that are returning cursor and dynamically created text, but I need to do it inside one function.
CREATE OR REPLACE FUNCTION get_students2(classId integer)
RETURNS refcursor
LANGUAGE plpgsql
AS
$
DECLARE
students text DEFAULT '';
student record;
cursor_students CURSOR(classId integer)
FOR SELECT firstName, surname
FROM tests.students
WHERE class = classId;
BEGIN
OPEN cursor_students(classId);
LOOP
FETCH cursor_students INTO student;
EXIT WHEN NOT FOUND;
students := students || ' ' || student.firstName || ' ' || student.surname;
END LOOP;
CLOSE cursor_students;
RETURN cursor_students;
END;
$
CREATE OR REPLACE FUNCTION get_students(classId integer)
RETURNS refcursor
LANGUAGE plpgsql
AS
$
DECLARE
students text DEFAULT '';
student record;
cursor_students CURSOR(classId integer)
FOR SELECT firstName, surname
FROM tests.students
WHERE class = classId;
BEGIN
OPEN cursor_students(classId);
LOOP
FETCH cursor_students INTO student;
EXIT WHEN NOT FOUND;
students := students || ' ' || student.firstName || ' ' || student.surname;
END LOOP;
CLOSE cursor_students;
RETURN students;
END;
$
I've tried to find how to do it and haven't come across any solution. I thought about making it inside a table, but I don't know if that's possible and didn't find anything about this.
I am not experienced with SQL at all, so don't know if such a thing is possible.
It's a task and the rules are that there needs to be a cursor, dynamic sql and function must return them both.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要将值返回到返回表的函数,我们必须在函数内包含返回查询语句。
To return value to a function that returns table, we have to include RETURN QUERY statement inside function.