如何在 plpgsql 的 IF 条件内运行 SQL 语句?我不想创建或替换函数。这是我尝试过的:
DO LANGUAGE plpgsql $$
BEGIN
IF 'Khosla' = 'Khosla' THEN
SELECT * FROM test_log limit 10;
ELSE
RAISE NOTICE 'not worked';
END IF;
END;
$$;
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 "inline_code_block" line 3 at SQL statement
我也试过了,但无法获取数据:
DO LANGUAGE plpgsql $$
BEGIN
if 'a' = 'a' THEN
PERFORM * FROM test_log limit 10;
else
raise notice 'not worked';
end if;
end;
$$;
Output: Query returned successfully with no result in 13 ms.
正确阅读文档后,我知道 PERFORM 执行 SQL 并丢弃结果集。
您能帮忙运行 SQL 语句并获取结果集吗?