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;
$$;
If you just want to test code snippets without going through all the hassle of building and dropping a function, then you can use
DO:You’ll need 9.0+ to use
DO.