Search code examples
sqlpostgresqlpostgresql-12

POSTGRES PROCEDURE with wildcard


I am trying to create a procedure with a wildcard with postgresql unfortunately I do not have any success. It looks like the postgresql versions have different ways of creating procedures, however, I find more help on SQL Server. Here the simple procedure I am trying to create:

create procedure getpeople (title varchar, city varchar)
LANGUAGE plpgsql 
as $$
select * from salesperson where job_title=title and city like % city

$$;

I am using postgreSQL12.1not


Solution

  • In addition to the answer by @Emilio Platzer: If you want to get multiple lines out of your function (not procedure) using PL/pgSQL, you can set it to return a table:

    CREATE OR REPLACE FUNCTION getpeople (varchar,varchar) 
    RETURNS TABLE (name text, job_title text, city text)
    AS $$
    BEGIN
    RETURN QUERY 
     (SELECT * FROM salesperson sp
      WHERE sp.job_title=$1 AND sp.city LIKE $2||'%');
    END  
    $$  LANGUAGE plpgsql;
    

    .. and call it like this:

    SELECT * FROM getpeople('ceo','D');
    

    Demo: db<>fiddle