错误 - 存储过程 PGSQL 中的“查询没有结果数据的目标”

Sku*_*mar 6 postgresql

我有以下存储过程:-

-- PROCEDURE: public.master_todo(text, text)

-- DROP PROCEDURE IF EXISTS public.master_todo(text, text);

CREATE OR REPLACE PROCEDURE public.master_todo(
    "actiontype" text,
    "actionvalue" text)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
IF(actiontype = 'getAllTodo') THEN
  SELECT * FROM todo_list;
  
END IF;
END
$BODY$;

Run Code Online (Sandbox Code Playgroud)

我使用 PGAdmin 中的过程 -> 创建过程创建的。但是,在使用 EXEC 测试它时,出现错误:-

ERROR:  query has no destination for result data
HINT:  If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT:  PL/pgSQL function master_todo(text,text) line 4 at SQL statement
SQL state: 42601
Run Code Online (Sandbox Code Playgroud)

在阅读了有关此问题的其他一些答案后,我尝试了:-

IF(actiontype = 'getAllTodo') THEN
  SELECT * FROM todo_list;
  RETURN;
END IF;
Run Code Online (Sandbox Code Playgroud)

IF(actiontype = 'getAllTodo') THEN
  RETURN SELECT * FROM todo_list;
  
END IF;
Run Code Online (Sandbox Code Playgroud)

两者都不起作用。第二个抛出了另一个错误,指出 RETURN 语句在存储过程中不能有任何参数。

到目前为止,本主题前面提到的所有答案都是基于函数的。但是,我无法将它与存储过程联系起来。

小智 6

过程并不意味着返回某些内容,因此无法返回选择的结果。

您需要使用设置返回函数:

CREATE OR REPLACE function public.master_todo("actiontype" text, "actionvalue" text)
  returns setof todo_list --<< this defines the structure of the result 
  LANGUAGE plpgsql
AS 
$BODY$
BEGIN
  IF (actiontype = 'getAllTodo') THEN
    return query 
       SELECT * FROM todo_list;
  END IF;
  
  -- what do you want to return if a different action type was passed? 
  return query 
    SELECT * 
    FROM todo_list
    WHERE ????;
END
$BODY$;
Run Code Online (Sandbox Code Playgroud)

然后像表格一样使用它:

select *
from master_todo('getAllTodo', '42');
Run Code Online (Sandbox Code Playgroud)