How to test an if statement in PostgreSQL?

18,291

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:

=> 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'll need 9.0+ to use DO.

Share:
18,291
Stefan Steiger
Author by

Stefan Steiger

I'm an avid HTTP-header-reader, github-user and a few more minor things like BusinessIntelligence & Web Software Developer Technologies I work with: Microsoft Reporting- & Analysis Service (2005-2016), ASP.NET, ASP.NET MVC, .NET Core, ADO.NET, JSON, XML, SOAP, Thrift ActiveDirectory, OAuth, MS Federated Login XHTML5, JavaScript (jQuery must die), ReverseAJAX/WebSockets, WebGL, CSS3 C#, .NET/mono, plain old C, and occasional C++ or Java and a little Bash-Scripts, Python and PHP5 I have a rather broad experience with the following relational SQL databases T-SQL PL/PGsql including CLR / extended stored procedures/functions Occasionally, I also work with MySQL/MariaDB Firebird/Interbase Oracle 10g+ SqLite Access I develop Enterprise Web-Applications (.NET 2.0 & 4.5) and interface to systems like LDAP/AD (ActiveDirectory) WebServices (including WCF, SOAP and Thrift) MS Federated Login OAuth DropBox XML & JSON data-stores DWG/SVG imaging for architecture In my spare-time, I'm a Linux-Server-Enthusiast (I have my own Web & DNS server) and reverse-engineer with interest in IDS Systems (IntrusionDetection), WireShark, IDA Pro Advanced, GDB, libPCAP. - Studied Theoretical Physics at the Swiss Federal Institute of Technology (ETHZ).

Updated on June 18, 2022

Comments

  • Stefan Steiger
    Stefan Steiger almost 2 years

    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;
    $$;
    
  • Stefan Steiger
    Stefan Steiger about 12 years
    It's only for development, so 9.0+ is OK.
  • a_horse_with_no_name
    a_horse_with_no_name about 12 years
    But if you have 9.0, there is no need to check for plpgsql (because it's installed by default). And for versions prior to 9.0 you do not have the DO statemen.t
  • mu is too short
    mu is too short about 12 years
    @a_horse_with_no_name: I think you're focusing on the wrong thing, the query itself is a distraction, the real problem is hidden at the end: "To test code with variables etc.". OP seems to be more interested in a REPL for psql.
  • Stefan Steiger
    Stefan Steiger about 12 years
    @a_horse_with_no_name: Wrong, you only do NOT need to check if deployment is going to be on your machine (if you know that it's 9.0+), but if you're also deploying on other machines, where version might be < 9.0... And the DO statement is really only for development (which is on 9.0), so it doesn't matter for deployment on < 9.0.