How to run SQL statement within an IF condition in plpgsql? I don't want to create or replace a function. This is what I tried:
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
I also tried this but was unable to get data:
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.
After reading document properly I came to know that PERFORM executes SQL and discards result set.
Can you help in running the SQL Statement and getting the result set?