Question: I want to test an if statement in PostgreSQL:
IF (SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql') > 0 THEN
PRINT 'Good'
ELSE
PRINT 'Bad'
END IF;
Now this throws an error at IF.
As far as I have read, this is because I need to use plpgsql to be able to use if, print, and variables.
So far, I probably also have to use SELECT instead of print as well.
How can I switch the language before executing this statement to plpgsql ?
I want to test it first, BEFORE I put it in a stored procedure. To test code with variables etc.
Edit:
Solved by:
DO LANGUAGE plpgsql $$
BEGIN
IF (SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql') > 0 THEN
RAISE NOTICE 'GOOD';
ELSE
RAISE NOTICE 'BAD';
END IF;
END;
$$;
Mainmethod?PRINTstatement in Postgres. And PL/pgSQL is always created since 9.0. So you only need to test for it, if you target previous versions