Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Unanswered: my first function

    Hello all. I am new to PostgreSQL and trying to write my first function. I followed the samples in documentation and I wrote the function below:

    Code:
    CREATE OR REPLACE FUNCTION TEST() RETURNS TABLE (today DATE) AS'
     DECLARE today DATE;
     BEGIN
        today = current_date;
     END;'
    LANGUAGE plpgsql;
    
    select * from TEST();
    Instead of "today=current_date" I also tried "select current_date into today", and several other things. Here, I expect to see a table with a column containing today's date. But I see an empty table. Could you please tell me what I am doing wrong? Thanks.

  2. #2
    Join Date
    Dec 2012
    Posts
    43
    Your function doesn't return a table because of the missing

    Code:
     RETURN QUERY SELECT ...
    Here is an example of a very small function:

    Code:
    CREATE OR REPLACE FUNCTION small_function() RETURNS date AS
    $BODY$
     DECLARE today DATE;
     BEGIN
      today = current_date -1;
      RETURN today;
     END;
    $BODY$
    LANGUAGE plpgsql;
    This function returns yesterday.
    You can test the new function:

    Code:
    thomas=> select small_function();
     small_function 
    ----------------
     2013-04-19
    (1 Zeile)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •