How to check if statement in PostgreSQL?

Question: I want to check if statement in PostgreSQL:

IF (SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql') > 0 THEN PRINT 'Good' ELSE PRINT 'Bad' END IF; 

Now this causes an error with IF.

As far as I read, this is because I need to use plpgsql to be able to use if, print and variables.

So far, I probably also had to use SELECT instead of printing. A.

How can I switch the language before executing this statement in plpgsql?

I want to test it first before I put it in a stored procedure. To check the code with variables, etc.


Edit:


It is decided:

 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; $$; 
+4
source share
1 answer

If you just want to test the code snippets without missing all the problems with creating and disabling the function, you can use DO

 => do language plpgsql $$ begin -- Yes, I have a table called pancakes in my playpen database. if (select count(*) from pancakes) > 0 then raise notice 'Got some'; else raise notice 'Got none'; end if; end; $$; 

You will need 9.0+ to use DO .

+9
source

Source: https://habr.com/ru/post/1394890/


All Articles