Reputation: 660
I trying to select query based on condition using IF ElSE in postgres. Below is my query.
DO
$do$
DECLARE res varchar(50) := 'a';
BEGIN
IF (res = 'a') THEN
SELECT "Name" FROM "TestTable";
ELSE
SELECT "ID" FROM "TestTable";
END IF;
END
$do$
but I am getting following error
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 5 at SQL statement
What I am doing wrong here??
Upvotes: 2
Views: 3663
Reputation: 17147
DO
purpose is to execute anonymous code block and it doesn't return anything (it returns void, to be specific).
You can execute your SELECT
statement afterwards (outside of DO
block), or perform an INSERT
to temporary table which you need to create beforehand (and this can be done within the block).
Upvotes: 3